Talk:cpp/language/decltype
From cppreference.com
For the following function, the function body is missing:
auto add(T t, U u) -> decltype(t + u); // return type depends on template parameters
if someone tries to run the code (as is) by calling add(), it will generate a compiler error.
It should have the following body: auto add(T t, U u) -> decltype(t + u) // return type depends on template parameters {
return t+u;
}
Shimon0505004 (talk) 13:52, 1 June 2017 (PDT)
- With a definition, the decltype becomes redundant since that return is deducible: it was there demonstrating the use in a declaration. But it's not technically wrong either way. --Cubbi (talk) 14:44, 1 June 2017 (PDT)
[edit] Parens and auto
Can we expand this page to clarify a few cases in particular?:
-
decltype(auto)
is important as it's the perfect-forwarding return type. - We mention it, but the distinction between
decltype((x))
anddecltype(x)
really isn't clear and could do with examples. It appears that if I haveint i;
thendecltype(i)
isint
anddecltype((i))
isint&
. - Even with
decltype((x))
, I believe there's a common use forstd::decay_t<decltype(x)>
, such as[](auto&& x) { using T = std::decay_t<decltype(x)>; ... }
to get the plain old type ofx
without any const or reference qualification.
BenFrantzDale (talk) 10:09, 7 September 2021 (PDT)
[edit] Wrong References
According to:
- C++17 standard (ISO/IEC 14882:2017)
- C++14 standard (ISO/IEC 14882:2014)
- C++11 standard (ISO/IEC 14882:2011)
id dcl.type.decltype
is absent in: (C++11), (C++14) and (C++17).
Section References should be corrected.