T
tonvandenheuvel
Hi all,
please consider the following piece of code:
#include <iostream>
template<typename T>
class A
{
public:
A(T t) { prop = t; };
bool operator!=(A<T> a) { return prop != a.prop; }
protected:
A() { };
T prop;
};
class B : public A<int>
{
public:
B(int i): A<int>(i) { };
};
class C : public A<double>
{
public:
C(double d): A<double>(d) { };
C(B b): A<double>(0) { };
};
int main(void)
{
B b(4);
C c(3);
if (c != b)
std::cout << "not equal" << std::endl;
else
std::cout << "equal" << std::endl;
}
I would expect b to be implicitly converted to an instance of C, such
that the != operator can be properly called. Instead, gcc complains as
follows:
implicit.cpp: In function 'int main()':
implicit.cpp:33: error: no match for 'operator!=' in 'c != b'
implicit.cpp:8: note: candidates are: bool A<T>:perator!=(A<T>)
[with T = double]
(tried with gcc 4.0.x & 4.1.x)
Any ideas why this fails? In case A is not templated, everything
compiles just fine.
Cheers,
Ton
please consider the following piece of code:
#include <iostream>
template<typename T>
class A
{
public:
A(T t) { prop = t; };
bool operator!=(A<T> a) { return prop != a.prop; }
protected:
A() { };
T prop;
};
class B : public A<int>
{
public:
B(int i): A<int>(i) { };
};
class C : public A<double>
{
public:
C(double d): A<double>(d) { };
C(B b): A<double>(0) { };
};
int main(void)
{
B b(4);
C c(3);
if (c != b)
std::cout << "not equal" << std::endl;
else
std::cout << "equal" << std::endl;
}
I would expect b to be implicitly converted to an instance of C, such
that the != operator can be properly called. Instead, gcc complains as
follows:
implicit.cpp: In function 'int main()':
implicit.cpp:33: error: no match for 'operator!=' in 'c != b'
implicit.cpp:8: note: candidates are: bool A<T>:perator!=(A<T>)
[with T = double]
(tried with gcc 4.0.x & 4.1.x)
Any ideas why this fails? In case A is not templated, everything
compiles just fine.
Cheers,
Ton