假设您尝试执行以下操作:
template</* args */>
typename std::enable_if< /*conditional*/ , /*type*/ >::type
static auto hope( /*args*/) -> decltype( /*return expr*/ )
{
}
是否可以将条件包含/重载(
std::enable_if
)与尾随返回类型(auto ... -> decltype()
)结合使用?我不会对使用预处理器的解决方案感兴趣。我总是可以做这样的事情
#define RET(t) --> decltype(t) { return t; }
并将其扩展为也考虑整个条件。相反,我感兴趣的是该语言是否支持它,而未使用其他特征作为返回类型,即
ReturnType<A,B>::type_t
或函数主体中使用的任何特征。 最佳答案
尾随返回类型与普通返回类型没有太大区别,只不过它是在参数列表和cv- / ref-限定符之后指定的。另外,它不一定需要decltype
,也可以使用普通类型:
auto answer() -> int{ return 42; }
因此,现在您应该看到问题的答案是:
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*/ )>>
{
}
尽管我个人更喜欢仅使用
decltype
和表达式SFINAE,但只要条件可以表示为表达式即可(例如,您可以在某种类型的对象上调用函数)吗:template<class T>
static auto hope(T const& arg)
-> decltype(arg.foo(), void())
{
// ...
}
关于c++ - 有可能使用尾随返回类型进行条件重载吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11866291/