( C++11)
cppreference.com
, .
|
, return. |
( C++14) |
|
, , , . |
( C++17) |
- () auto
|
(1) | ( C++11) | |||||||
- () decltype(auto)
|
(2) | ( C++14) | |||||||
| - | ( C++20) , , , <>
|
auto , const &, . decltype(auto) . ( C++14)
:
- :
auto x = ;. .
auto-auto( C++20), ( ).
,const auto& i = ;,iutemplate<class U> void f(const U& u),f(). ,auto&&lvalue, rvalue , for .
decltype(auto)-decltype(auto)( C++20),decltype(),.( C++14) , . ,
auto i = 0, d = 0.0;,auto i = 0, *p = &i;autoint. - new. .
new T(T-, , ),T,xT x ;. - ( C++14) -:
auto& f();. ( C++17) return.
. - ( C++17) :
template<auto I> struct A;. .
| ( C++23) |
|
,
|
( C++14) |
, |
( C++20) |
auto , auto f() -> int, i = 0; .
auto , , ( -).
auto (*p)() -> int; // p , int
auto (*q)() -> auto = p; // q , T,
// T p
| ( C++17) |
| ( ) |
__cpp_decltype_auto |
201304L |
(C++14) | decltype(auto)
|
#include <iostream>
#include <utility>
template<class T, class U>
auto add(T t, U u) { return t + u; } // operator+(T, U)
// decltype(auto)
// ,
template<class F, class... Args>
decltype(auto) PerfectForward(F fun, Args&&... args)
{
return fun(std::forward<Args>(args)...);
}
template<auto n> // C++17
auto f() -> std::pair<decltype(n), decltype(n)> // auto
//
{
return {n, n};
}
int main()
{
auto a = 1 + 2; // a int
auto b = add(1, 1.2); // b double
static_assert(std::is_same_v<decltype(a), int>);
static_assert(std::is_same_v<decltype(b), double>);
auto c0 = a; // c0 int, a
decltype(auto) c1 = a; // c1 int, a
decltype(auto) c2 = (a); // c2 int&, a
std::cout << " c2, a = " << a << '\n';
++c2;
std::cout << " c2, a = " << a << '\n';
auto [v, w] = f<0>(); //
auto d = {1, 2}; // OK: d std::initializer_list<int>
auto n = {5}; // OK: n std::initializer_list<int>
// auto e{1, 2}; // DR n3922, std::initializer_list<int>
auto m{5}; // OK: m int DR n3922, initializer_list<int>
// decltype(auto) z = { 1, 2 } // : {1, 2}
// auto , -
auto lambda = [](int x) { return x + 3; };
// auto int x; // C++98, C++11
// auto x; // C, C++
[](...){}(c0, c1, v, w, d, n, m, lambda); //
// " "
}
:
c2, a = 3
c2, a = 4
C++:
| CWG 1265 | C++11 | auto |
|
| CWG 1346 | C++11 | ||
| CWG 1347 | C++11 | auto T std::initializer_list<T>
|
|
| CWG 1852 | C++14 | auto decltype(auto)
|
|