I'm writing a function which takes a universal reference and want to give it a default type and value.
This code works.
template <class T = myclass>
void f(T&& t = T{});
Is this more proper way to do it?
template <class T = myclass>
void f(T&& t = typename std::remove_reference<T>::type{});
T
will be either one of otherclass&
, const otherclass&
, otherclass
, and it seems that it's not proper to default construct from the reference type.
otherclass o;
const otherclass co;
f(o); // T will be 'otherclass&'
f(co); // T will be 'const otherclass&'
f(otherclass{}); // T will be 'otherclass'
I checked if I could construct from the reference type and got erros from this code.
using ref = otherclass&;
otherclass a = ref{};
error: invalid cast of an rvalue expression of type ‘otherclass’ to type ‘ref {aka otherclass&}’
Can anyone help me understand it?
Aucun commentaire:
Enregistrer un commentaire