This is the mail archive of the gcc-bugs@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++: member template assignment operator


Hello, 

Below follows a bug report related to defining a const templated
assignment operator.
(There are reasons for defining such an operator, so the issue is not
entirely academic.)

Version: GCC version 2.95.2
Sustem: Debian Linux in Intel Pentium 

Test program:

// ---------------------------------------------------
#include <iostream>

using std::cout;

struct  A {
  int i;

#ifdef ERROR1
  template<class T>
  void operator=(const T& t)  const { 
    cout << "This should be called?"; 
  }
#endif

};


int main()
{

  const A a = A();
  A b;

  b.i = 100;

  cout << "A : " << a.i << endl;

  a = b; 

  cout << "A : " << a.i << endl;

}



Options: 
compiled with 'gcc -lstdc++' the compiler reports the error (from line a
= b;):
 
>test_code.cpp: In function `int main()':
>test_code.cpp:25: passing `const A' as `this' argument of `struct A & >A::operator =(const A &)' discards qualifiers

as it should.

But when compiled with 'gcc -lstdc++ -DERROR1' the compiler accepts the
code.
The output of the program is:
A : 0
A : 100 

Hence, adding a const template assignment operator seems to make it
possible to call the implicitely defined assignment operator for a const
object.

My intepretation is that the line
a = b should succeed but the template operatror should be called.

Best Regards

/Jaakko Järvi
-- 
--- Jaakko Järvi, jaakko.jarvi@cs.utu.fi
--- Turku Centre for Computer Science (www.tucs.fi)
--- Lemminkäisenkatu 14 A, FIN-20520 Turku, Finland
--- Phone: +358-2-333 8656, Fax: +358-2-333 8600

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