This is the mail archive of the gcc-prs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

c++/110: Re: casting to char arrays produces strange effects



>Number:         110
>Category:       c++
>Synopsis:       casting to char arrays produces strange effects
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          analyzed
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Wed Mar 15 01:06:00 PST 2000
>Closed-Date:
>Last-Modified:
>Originator:     Robert Karban <rkarban@eso.org>
>Release:        2.95.2
>Organization:
ESO-Garching
>Environment:
>Description:
 Original-Message-ID: <37B53FDF.38CE60A8@eso.org>
 Date: Sat, 14 Aug 1999 12:07:28 +0200
 

 problem:
 =======
 If a const cast in a template class is defined and
 used as simple cast without const qualifier then
 the cast operator is NOT called when the type is
 a character array. Instead something else is called.

 If the instantiated type is a basic type it works.
 This is bad if you rely on calling the operator.

 Does anyone know what's wrong here?
 The behaviour doesn't seem to be logical nor consistent.

 Thanks for your help.
 Robert Karban
 rkarban@eso.org

 gcc version:
 ===========
 Reading specs from
 /vlt/gnu-exp/lib/gcc-lib/hppa1.1-hp-hpux10.20/2.95/specs
 gcc version 2.95 19990728 (release)

 gcc command line:
 ================
 gcc -ansi -Wall cast2.C -o cast2 -lstdc++

 system:
 ======
 HP-UX 10.20

 code:
 ====
 #include <stdio.h>
 #include <stdlib.h>
 #include <iostream.h>

 typedef unsigned char      BYTES20[20];
 typedef int                INT32;
 typedef unsigned int       UINT32;

 template<class T>
 class TEST
 {
   public:
     T                 value;

     TEST(T newVal) { value = newVal; }

     operator  const T&()
     {
  cout << "====> in operator const()\n";
  return value;
     }
 };

 int main(int, char *argv[])
 {
     {
     cout << "\n===== Testing INT32 ======\n";

     TEST<INT32> vector(123);

     cout << "cast value without const (why is const operator called)\n";

     cout << " " << (INT32) vector;
     cout << "\n";
     }

     {
     cout << "\n===== Testing BYTES20 ======\n";

     BYTES20 array;
     strcpy((char*) array, "456");

     TEST<BYTES20> vector(array);

     cout << "cast value without const (what cast is called)\n";
     cout << " " << (BYTES20&) vector;
     cout << "\n";

     cout << "cast value with const\n";
     strcpy((char*) array, "000");
     strcpy((char*) array, (const char *)(const BYTES20&) vector);
     cout << "value =  " << array;

     cout << "\ncast value without const (operator not called!)\n";
     strcpy((char*) array, "000");
     strcpy((char*) array, (const char *)(BYTES20&) vector);
     cout << "value =  " << array;

     cout << "\nEND\n";
     }
     cout.flush();
 } /* end main() */

 Output:
 ======
 ===== Testing INT32 ======
 cast value without const (why is const operator called)
  ====> in operator const()
 123

 ===== Testing BYTES20 ======
 cast value without const (what cast is called)
  456
 cast value with const
 ====> in operator const()
 value =  456
 cast value without const (operator not called!)
 value =  456
 END


>How-To-Repeat:
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]