Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decltype(auto) vs auto&& to conserve cv qualifier

Tags:

c++11

c++14

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()
like image 239
Guillaume Paris Avatar asked Nov 19 '13 08:11

Guillaume Paris


People also ask

What is the difference between auto and decltype Auto?

'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.

What is decltype function?

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.

What is C++ Auto?

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.

Which is the default return type specifier in C Plus Plus?

I have recently read that the default return type for all functions in C++ is int .


1 Answers

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.

like image 157
Potatoswatter Avatar answered Sep 18 '22 11:09

Potatoswatter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!