P
petschy
hello,
i've run into an error when qualifying a copy ctor 'explicit'. the
strange thing is that i get a compiler error only if the class is a
template and declare the variable as X<Z> x = y. X<Z> x(y) is fine.
Tested with gcc 2.95, 3.3, 4.1, all gave the same error:
t.cpp: In function 'int main()':
t.cpp:44: error: no matching function for call to
'D<int>:
(D<int>&)'
which is quite right, since there is only D(const D&). the thing i
don't understand is why this error only occures if the class is a
template, and if used as D<int> d3 = d, since D<int> d2(d) compiles
fine.
is there a difference between the variable declarations A a(b) and A a
= b? to the best of my knowledge, not, and the compilers seem to
generate the same code for both.
and for the ones asking why i qualify the copy ctor 'explicit' : it's
not needed, but all other ctors were explicit, so i thought, it look
consistent that way, then ran into this issue.
regards, p
here's the code:
class A
{
public:
A() { }
A(const A&) { }
};
class B
{
public:
explicit B() { }
explicit B(const B&) { }
};
template<typename T>
class C
{
public:
C() { }
C(const C&) { }
};
template<typename T>
class D
{
public:
explicit D() { }
explicit D(const D&) { }
};
int main()
{
A a;
B b;
C<int> c;
D<int> d;
A a2(a);
B b2(b);
C<int> c2(c);
D<int> d2(d);
C<int> c3 = c;
D<int> d3 = d; // <--- compiler error
return 0;
}
i've run into an error when qualifying a copy ctor 'explicit'. the
strange thing is that i get a compiler error only if the class is a
template and declare the variable as X<Z> x = y. X<Z> x(y) is fine.
Tested with gcc 2.95, 3.3, 4.1, all gave the same error:
t.cpp: In function 'int main()':
t.cpp:44: error: no matching function for call to
'D<int>:
which is quite right, since there is only D(const D&). the thing i
don't understand is why this error only occures if the class is a
template, and if used as D<int> d3 = d, since D<int> d2(d) compiles
fine.
is there a difference between the variable declarations A a(b) and A a
= b? to the best of my knowledge, not, and the compilers seem to
generate the same code for both.
and for the ones asking why i qualify the copy ctor 'explicit' : it's
not needed, but all other ctors were explicit, so i thought, it look
consistent that way, then ran into this issue.
regards, p
here's the code:
class A
{
public:
A() { }
A(const A&) { }
};
class B
{
public:
explicit B() { }
explicit B(const B&) { }
};
template<typename T>
class C
{
public:
C() { }
C(const C&) { }
};
template<typename T>
class D
{
public:
explicit D() { }
explicit D(const D&) { }
};
int main()
{
A a;
B b;
C<int> c;
D<int> d;
A a2(a);
B b2(b);
C<int> c2(c);
D<int> d2(d);
C<int> c3 = c;
D<int> d3 = d; // <--- compiler error
return 0;
}