I'm trying to mimic the behavior of inheritable constructors, since they are yet to be implemented in g++.
I tried the techniques described here https://stackoverflow.com/a/5411992/234261 but I'm running into issues when I want to add my own constructors. Specifically, I am trying to add a specialized copy constructor, as well as inheriting all other constructors. Unfortunately, the inherited, more generic, copy constructor is always called instead.
An example
#include <iostream>
struct A {
A(){}
A(const A &){ std::cout << "IN A" << std::endl; }
};
struct B : public A{
template<typename ...Args,
typename = typename std::enable_if
<
std::is_constructible<A, Args...>::value
>::type>
B(Args &&...args)
: A(std::forward<Args>(args)...) {}
// These don't work either.
//B(const A &a):A(a){ std::cout << "IN B-A" << std::endl; }
//B(const B &b):A(b){ std::cout << "IN B-B" << std::endl; }
};
template<>
B::B(const A &a):A(a){ std::cout << "IN B-A" << std::endl; }
template<>
B::B(const B &b):A(b){ std::cout << "IN B-B" << std::endl; }
int main(){
A a; // 1. Prints nothing as expected
B b(a); // 2. Prints "IN A" only
B b1(b); // 3. Prints "IN A" only
return 0;
}
I would like [3] to print IN A and then IN B-B. Somehow I have managed to get [2] to work in my actual code, but can't repeat it in this small example for some reason.
I understand that the template constructor is being created due to the fact that the subclass(B) can actually be used to construct the superclass(A).
Why, however, does my explicit specialization not get invoked? Am I specializing incorrectly? Is there a better way?
Link to this example if anyone wants to run it http://ideone.com/eUHD5
I'm using gcc 4.6