auto、decltype(auto) 和尾随 return 类型
auto, decltype(auto) and trailing return type
有区别吗:
template <class T>
constexpr decltype(auto) f(T&& x) -> decltype(std::get<0>(std::forward<T>(x)))
{
return std::get<0>(std::forward<T>(x));
}
和:
template <class T>
constexpr auto f(T&& x) -> decltype(std::get<0>(std::forward<T>(x)))
{
return std::get<0>(std::forward<T>(x));
}
如果是的话,它是什么,我应该使用哪个来完美转发?
尾随 return 类型只能与 auto
一起使用
decltype(auto)
vs auto
的重点是distinguish the case whether the return type should be a reference or value。但是在您的情况下,return 类型已经明确定义为 decltype(std::get<0>(std::forward<T>(x)))
,因此即使您使用 auto
.
,它也会被完美转发
在auto f() -> T
中,"auto"关键字只是一个syntactic construct to fill in a type position。它没有其他用途。
事实上,在 C++17 中,您 不能 将 decltype(auto)
与尾随 return 类型一起使用。
C++14 措辞 (n3936 §7.1.6.4[dcl.spec.auto]/1):
The auto
and decltype(auto)
type-specifiers designate a placeholder type that will be replaced later, either by deduction from an initializer or by explicit specification with a trailing-return-type. The auto
type-specifier is also used to signify that a lambda is a generic lambda.
C++17 措辞 (n4618 §7.1.7.4[dcl.spec.auto]/1):
The auto
and decltype(auto)
type-specifiers are used to designate a placeholder type that will be replaced later by deduction from an initializer. The auto
type-specifier is also used to introduce a function type having a trailing-return-type or to signify that a lambda is a generic lambda (5.1.5). The auto
type-specifier is also used to introduce a decomposition declaration (8.5).
这是DR 1852, see Does a placeholder in a trailing-return-type override an initial placeholder?。
实际上,虽然 gcc
接受 decltype(auto) f() -> T
(which is a bug),但 clang
会 拒绝 它说
error: function with trailing return type must specify return type 'auto',
not 'decltype(auto)'
有区别吗:
template <class T>
constexpr decltype(auto) f(T&& x) -> decltype(std::get<0>(std::forward<T>(x)))
{
return std::get<0>(std::forward<T>(x));
}
和:
template <class T>
constexpr auto f(T&& x) -> decltype(std::get<0>(std::forward<T>(x)))
{
return std::get<0>(std::forward<T>(x));
}
如果是的话,它是什么,我应该使用哪个来完美转发?
尾随 return 类型只能与 auto
一起使用
decltype(auto)
vs auto
的重点是distinguish the case whether the return type should be a reference or value。但是在您的情况下,return 类型已经明确定义为 decltype(std::get<0>(std::forward<T>(x)))
,因此即使您使用 auto
.
在auto f() -> T
中,"auto"关键字只是一个syntactic construct to fill in a type position。它没有其他用途。
事实上,在 C++17 中,您 不能 将 decltype(auto)
与尾随 return 类型一起使用。
C++14 措辞 (n3936 §7.1.6.4[dcl.spec.auto]/1):
The
auto
anddecltype(auto)
type-specifiers designate a placeholder type that will be replaced later, either by deduction from an initializer or by explicit specification with a trailing-return-type. Theauto
type-specifier is also used to signify that a lambda is a generic lambda.
C++17 措辞 (n4618 §7.1.7.4[dcl.spec.auto]/1):
The
auto
anddecltype(auto)
type-specifiers are used to designate a placeholder type that will be replaced later by deduction from an initializer. Theauto
type-specifier is also used to introduce a function type having a trailing-return-type or to signify that a lambda is a generic lambda (5.1.5). Theauto
type-specifier is also used to introduce a decomposition declaration (8.5).
这是DR 1852, see Does a placeholder in a trailing-return-type override an initial placeholder?。
实际上,虽然 gcc
接受 decltype(auto) f() -> T
(which is a bug),但 clang
会 拒绝 它说
error: function with trailing return type must specify return type 'auto',
not 'decltype(auto)'