- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
考虑以下代码:
template<typename F>
struct S;
template<typename Ret, typename... Args>
struct S<Ret(Args...)> { };
template<typename... Args>
using Alias = S<void(Args...)>;
int main() {
S<void(int)> s;
Alias<int> alias;
}
它工作正常,正如预期的那样,涉及 S
的行和一个涉及Alias
在引擎盖下定义相同的类型 S<void(int)>
.
现在,考虑以下更改:
int main() {
S<void(void)> s; // this line compiles
Alias<void> alias; // this line does not
}
出于与上述类似的原因,我希望它能够编译。
不用说,由于涉及 Alias
的行,它无法编译。 ,相反我得到错误:
In substitution of 'template using Alias = S [with Args = {void}]'
[...]
error: invalid parameter type 'void'
问题很简单:我在这里错过了什么?
最佳答案
来自 [dcl.fct],强调我的:
A parameter list consisting of a single unnamed parameter of non-dependent type
void
is equivalent to an empty parameter list. Except for this special case, a parameter shall not have type cvvoid
.
在这种情况下,Args...
是依赖类型包,所以 void
那里是不允许的。 [temp.deduct] 中的注释中重复了这个想法:
[ Note: Type deduction may fail for the following reasons:
— [...]
— Attempting to create a function type in which a parameter has a type ofvoid
, or in which the return type is a function type or array type.
— [...]
—end note ]
请注意 S<void(void)>
自 void(void)
开始编译是非依赖的,相当于 void()
, 所以 Ret(Args...)
永远不会被推导出有 void
在参数列表中 - 它是用 Args...
推导的空的。
至少有一个简单的解决方法,你可以只写 Alias<>
.
关于c++ - 别名模板、偏特化和无效参数类型 void,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35879510/
我是一名优秀的程序员,十分优秀!