考虑下面的代码:decltype(auto),追踪返回类型和sfinae:我们可以混合它们吗?
auto f() -> decltype(auto) { /* do whatever you want here */ }
int main() { f(); }
返回类型推导并decltype(auto)
用作后返回类型。
下面的代码是一个稍微修改的(实际上,sfinae'd)版本:
struct S { static void f() {} };
struct T {};
template<typename U>
auto f(int) -> decltype(U::f(), void()) {
// do whatever you want here
}
template<typename>
auto f(char) -> decltype(auto) {
// do whatever you want here
}
int main() {
f<S>(0);
f<T>(0);
}
如果你在考试采取这样的功能:
template<typename U>
auto f(int) -> decltype(U::f(), void()) {
// do whatever you want here
}
的问题是:是否有可能使用尾随返回类型来做sfinae并且仍然具有推导出的返回类型?
我的意思是像下面的代码(不工作,当然):
template<typename U>
auto f(int) -> decltype(U::f(), auto) {
// do whatever you want here
}
注:我不是在寻找涉及模板参数的替代方法,我知道他们和我”米只是好奇,知道如果这是一个可行的解决方案。
不要以为这是可能的,但你想要的类型推断为'auto'或者'decltype(auto)'? – Holt
不要以为这里有什么可以做的。特意扣除退货类型不要SFINAE。 – Xeo
@Holt好问题,我会说在第一个例子中有'decltype(auto)',但是如果你知道如何将它推断为'auto',它也可以是一个很好的答案。 – skypjack