The following does not compile with g ++ 4.4.7, with -std == C ++ 0x passed on the command line:
template <typename T>
class A
{
public:
T v;
A() { std::cout << "A default constructor\n"; }
A(const A& i): v(i.v) { std::cout << "A copy constructor\n"; }
A(A&& i): v(std::move(i.v)) { std::cout << "A move constructor\n"; }
template <typename V>
explicit A(const V& i): v(i) {}
};
class B: public A<int>
{
public:
B() { std::cout << "B default constructor\n"; }
B(const B& i): A<int>(i) {
std::cout << "B copy constructor\n"; }
B(B&& i): A<int>(std::move(i)) {
std::cout << "B move constructor\n"; }
B(const B& i): A<int>(static_cast<const A<int> &>(i)) {
std::cout << "B copy constructor\n"; }
B(B&& i): A<int>(std::move(static_cast<A<int> &&>(i))) {
std::cout << "B move constructor\n"; }
};
B foo() {
B t;
return t;
}
int main() {
B t(foo());
B t2(std::move(t));
std::cout << "Result is " << t2.v << std::endl;
return 0;
}
It throws the following error, where apparently the copy and move constructors cause an explicit pattern A, rather than the obvious copy / move constructors A:
container.cpp: In constructor ‘A<T>::A(const V&) [with V = B, T = int]’:
container.cpp:26: instantiated from here
container.cpp:17: error: cannot convert ‘const B’ to ‘int’ in initialization
Disabling the first one #if 1deletes the template, and it compiles and produces the expected calls. Disabling the second #if 1also makes it work (however, doing the equivalent in our code would be painful and would require a lot of careful editing).
I currently get around this by changing the template constructor to this and adding a bool for all users, but this is ugly:
A(const V&i, bool dummy): v(i) {}
Questions:
- Is this a bug in GCC?
- If not, can someone explain why this is happening?
- ? static_casts?