rit*_*ter 1 c++ templates sfinae c++11 trailing-return-type
假设您尝试执行以下操作:
template</* args */>
typename std::enable_if< /*conditional*/ , /*type*/ >::type
static auto hope( /*args*/) -> decltype( /*return expr*/ )
{
}
Run Code Online (Sandbox Code Playgroud)
是否可以将条件包含/重载(std::enable_if)与trailing-return-type(auto ... -> decltype())结合起来?
在使用预处理器的解决方案中,我不会感兴趣.我总是可以这样做
#define RET(t) --> decltype(t) { return t; }
Run Code Online (Sandbox Code Playgroud)
并扩展它以采取整个条件.相反,如果语言支持它而不使用返回类型的其他特征,即ReturnType<A,B>::type_t函数体中使用的任何特性,我感兴趣.
该尾返回类型是不正常的返回类型太大的不同,不同之处在于它的参数列表和CV-/ REF-预选赛之后指定.此外,它不一定需要decltype,普通类型也可以:
auto answer() -> int{ return 42; }
Run Code Online (Sandbox Code Playgroud)
那么到现在为止你应该看到你的问题的答案是:
template<class T>
using Apply = typename T::type; // I don't like to spell this out
template</* args */>
static auto hope( /*args*/)
-> Apply<std::enable_if</* condition */, decltype( /*return expr*/ )>>
{
}
Run Code Online (Sandbox Code Playgroud)
虽然我个人更喜欢使用just decltype和表达式SFINAE,只要条件可以表达为表达式(例如,你可以在某种类型的对象上调用函数):
template<class T>
static auto hope(T const& arg)
-> decltype(arg.foo(), void())
{
// ...
}
Run Code Online (Sandbox Code Playgroud)