decltype vs auto
Solution 1:
decltype
gives the declared type of the expression that is passed to it. auto
does the same thing as template type deduction. So, for example, if you have a function that returns a reference, auto
will still be a value (you need auto&
to get a reference), but decltype
will be exactly the type of the return value.
#include <iostream>
int global{};
int& foo()
{
return global;
}
int main()
{
decltype(foo()) a = foo(); //a is an `int&`
auto b = foo(); //b is an `int`
b = 2;
std::cout << "a: " << a << '\n'; //prints "a: 0"
std::cout << "b: " << b << '\n'; //prints "b: 2"
std::cout << "---\n";
decltype(foo()) c = foo(); //c is an `int&`
c = 10;
std::cout << "a: " << a << '\n'; //prints "a: 10"
std::cout << "b: " << b << '\n'; //prints "b: 2"
std::cout << "c: " << c << '\n'; //prints "c: 10"
}
Also see David Rodríguez's answer about the places in which only one of auto
or decltype
are possible.
Solution 2:
auto
(in the context where it infers a type) is limited to defining the type of a variable for which there is an initializer. decltype
is a broader construct that, at the cost of extra information, will infer the type of an expression.
In the cases where auto
can be used, it is more concise than decltype
, as you don't need to provide the expression from which the type will be inferred.
auto x = foo(); // more concise than `decltype(foo()) x`
std::vector<decltype(foo())> v{ foo() }; // cannot use `auto`
The keyword auto
is also used in a completely unrelated context, when using trailing return types for functions:
auto foo() -> int;
There auto
is only a leader so that the compiler knows that this is a declaration with a trailing return type. While the example above can be trivially converted to old style, in generic programming it is useful:
template <typename T, typename U>
auto sum( T t, U u ) -> decltype(t+u)
Note that in this case, auto
cannot be used to define the return type.
Solution 3:
modify @Mankarse's example code,I think a better one blew:
#include <iostream>
int global = 0;
int& foo()
{
return global;
}
int main()
{
decltype(foo()) a = foo(); //a is an `int&`
auto b = foo(); //b is an `int`
b = 2;
std::cout << "a: " << a << '\n'; //prints "a: 0"
std::cout << "b: " << b << '\n'; //prints "b: 2"
std::cout << "global: " << global << '\n'; //prints "global: 0"
std::cout << "---\n";
//a is an `int&`
a = 10;
std::cout << "a: " << a << '\n'; //prints "a: 10"
std::cout << "b: " << b << '\n'; //prints "b: 2"
std::cout << "global: " << global << '\n'; //prints "global: 10"
return 0;
}