For example, simple identity functor:
template <typename T>
class identity
{
public:
constexpr auto operator ()(T && i) -> decltype(std::forward<T>(i))
{
return std::forward<T>(i);
}
};
What is better (C++14 and newer) for return value:
-> decltype(std::forward<T>(i)) or-> decltype(auto)Or are they the same?
Or are they the same?
Assuming you write it correctly:
constexpr decltype(auto) operator ()(T && i)
{
return std::forward<T>(i);
}
They are the same. [dcl.type.auto.deduct]:
A type
Tcontaining a placeholder type, and a corresponding initializere, are determined as follows:
- for a non-discarded return statement that occurs in a function declared with a return type that contains a placeholder type,
Tis the declared return type andeis the operand of the return statement. If the return statement has no operand, theneisvoid();If the placeholder is the
decltype(auto)type-specifier,Tshall be the placeholder alone. The type deduced forTis determined as described in [dcl.type.simple], as thoughehad been the operand of thedecltype
The return type of the function is deduced from return e; as though by decltype(e). So it would be the same as an explicit decltype(std::forward<T>(i)).
What is better
In this case, I'll go with "less is more". decltype(auto) gives you what you are after with less verbosity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With