M
Marc
See below for the code. A variadic argument U&&... matches a
copy-from-lvalue better than the implicit copy constructors, so I need
to declare a copy constructor from lvalue. And since its existence
prevents the implicit declaration of the other two versions (from const
ref and from rvalue), I need to declare those 2 as well. Now I don't
want anything fancy for those and I am fine with whatever the compiler
can generate by default, so I default the three copy/move constructors.
Now, according to g++, I am only allowed to specify "=default" inside
the class for 2 of them, for the one that takes an lvalue reference I
can only do it outside the class (though I can add "inline" if I like),
otherwise I get:
error: ‘A::A(A&)’ declared to take non-const reference cannot be
defaulted in the class body
Now I am quite confused as to why that is...
#include <iostream>
struct A {
A()=default;
template<class...U> A(U&&...){
std::cout << "NOOOOO!\n";
}
A(A&);
A(A const&)=default;
A(A &&)=default;
};
A::A(A&)=default;
int main(){
A a;
std::cout << "---\n";
A b{a};
std::cout << "---\n";
A c{(A&)a}; // same as the previous one
std::cout << "---\n";
A d{(A const&)a};
std::cout << "---\n";
A e{(A&&)a};
}
copy-from-lvalue better than the implicit copy constructors, so I need
to declare a copy constructor from lvalue. And since its existence
prevents the implicit declaration of the other two versions (from const
ref and from rvalue), I need to declare those 2 as well. Now I don't
want anything fancy for those and I am fine with whatever the compiler
can generate by default, so I default the three copy/move constructors.
Now, according to g++, I am only allowed to specify "=default" inside
the class for 2 of them, for the one that takes an lvalue reference I
can only do it outside the class (though I can add "inline" if I like),
otherwise I get:
error: ‘A::A(A&)’ declared to take non-const reference cannot be
defaulted in the class body
Now I am quite confused as to why that is...
#include <iostream>
struct A {
A()=default;
template<class...U> A(U&&...){
std::cout << "NOOOOO!\n";
}
A(A&);
A(A const&)=default;
A(A &&)=default;
};
A::A(A&)=default;
int main(){
A a;
std::cout << "---\n";
A b{a};
std::cout << "---\n";
A c{(A&)a}; // same as the previous one
std::cout << "---\n";
A d{(A const&)a};
std::cout << "---\n";
A e{(A&&)a};
}