sky*_*ack 8 c++ templates decltype sfinae language-lawyer
请考虑以下代码:
auto f() -> decltype(auto) { /* do whatever you want here */ }
int main() { f(); }
Run Code Online (Sandbox Code Playgroud)
推导出返回类型,并将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);
}
Run Code Online (Sandbox Code Playgroud)
如果你考试这个功能:
template<typename U>
auto f(int) -> decltype(U::f(), void()) {
// do whatever you want here
}
Run Code Online (Sandbox Code Playgroud)
问题是:是否可以使用尾随返回类型来执行sfinae并仍然推导出返回类型?
我的意思是类似下面的代码(当然不起作用):
template<typename U>
auto f(int) -> decltype(U::f(), auto) {
// do whatever you want here
}
Run Code Online (Sandbox Code Playgroud)
注意:我不是在寻找涉及模板参数的替代方法,我知道它们,我只是想知道这是否是一个可行的解决方案.
decltype(auto)是一个不可分割的构造(几乎就像它是关键字一样decltype_auto).除此之外,auto不能用作内部的独立实体decltype(x),因为这将阻止x成为有效的表达式.
| 归档时间: |
|
| 查看次数: |
300 次 |
| 最近记录: |