I have a class template with template parameter T and a member of type T. I want to initialize that member with a parameter passed to the ctor and I also want the passed parameter to be moved if it's a rvalue reference and if T supports move semantic:
template <typename T>
class C {
public:
explicit C(T t) : t_(t)
{
}
explicit C(T&& t) : t_(std::move(t))
{
}
...
private:
T t_;
};
g++ 4.8 gives the following error if I try to pass rvalue reference to the ctor:
int main()
{
int x = 0;
C<int> p1{x}; // OK
C<int> p2{1}; // error g++-4.8: call of overloaded ‘C(<brace-enclosed initializer list>)’ is ambiguous
return 0;
}
The full error text:
g++-4.8 -std=c++11 -O2 -Wall -pedantic -pthread main.cpp main.cpp: In function ‘int main()’: main.cpp:23:16: error: call of overloaded ‘C()’ is ambiguous C p2{1}; // error g++-4.8: call of overloaded ‘C()’ is ambiguous ^ main.cpp:23:16: note: candidates are: main.cpp:12:11: note: C::C(T&&) [with T = int] explicit C(T&& t) : t_(std::move(t)) ^ main.cpp:8:14: note: C::C(T) [with T = int] explicit C(T t) : t_(t) ^ main.cpp:6:7: note: constexpr C::C(const C&) class C { ^ main.cpp:6:7: note: constexpr C::C(C&&)
Could someone help me, please? Thanks!