Is there an advantage to use the c++14 feature decltype(auto) vs auto&& for keeping cv qualifier ?
auto v = const_return_func() //strip the constness
auto&& v = const_return_func()
decltype(auto) = const_return_func()
'auto' lets you declare a variable with a particular type whereas decltype lets you extract the type from the variable so decltype is sort of an operator that evaluates the type of passed expression.
The decltype type specifier yields the type of a specified expression. The decltype type specifier, together with the auto keyword, is useful primarily to developers who write template libraries. Use auto and decltype to declare a function template whose return type depends on the types of its template arguments.
The auto keyword in C++ automatically detects and assigns a data type to the variable with which it is used. The compiler analyses the variable's data type by looking at its initialization. It is necessary to initialize the variable when declaring it using the auto keyword.
I have recently read that the default return type for all functions in C++ is int .
Supposing the function returns a const & reference, since const object and const && are fairly useless return types, decltype(auto) will do the same thing as auto &&.
You should generally not use decltype(auto) for local variables as it has no particular use case, and obscures whether the declared entity is an object or a reference.
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