This question is a follow up to this one: Explicit template specialization for templated constructor of templated class The answers given in the other question are of course right but it turned out that I was not quite asking what I wanted to ask - so here is a new question:
Consider the following code:
template<typename First, typename ... Rest> class var {
public:
var() {
std::cout << "default" << std::endl;
}
var(const var& v) {
std::cout << "copy" << std::endl;
}
var(var&& v) {
std::cout << "move" << std::endl;
}
template<typename T>
var(const T& t) {
std::cout << "general lvalue" << std::endl;
}
template<typename T>
var(T&& t) {
std::cout << "general rvalue" << std::endl;
}
};
int main()
{
var<int> i0; // expect 'default' -> get 'default'
var<int> i1(i0); // expect 'copy' -> get 'general rvalue'
var<int> i2(std::move(i0)); // expect 'move' -> get 'move'
std::string s("Hello");
var<int> i3(s); // expect 'general lvalue' -> get 'general rvalue'
var<int> i4(std::move(s)); // expect 'general rvalue' -> get 'general rvalue'
}
I wrote in the main function which constructors I expect and want to be called and which ones are actually called. Here are my questions:
1) Can you explain why the program does not behave as I expected?
2) How can I make the program to call the copy and move constructor of var when it gets a var and the templated constructors otherwise?
3) And finally, I'm trying to put the two templated constructors into one handling both lvalues and rvalues and forwarding them to another function using std::forward - how could this look like?