问题描述
template <class _Func,class _Ret>
struct _STL_GENERATOR_ERROR {
static _Ret __generator_requirement_violation(_Func& __f) {
return __f();
}
};
template <class _Func>
struct _STL_GENERATOR_ERROR<_Func,void> {
static void __generator_requirement_violation(_Func& __f) {
return __f();
}
};
用于检查相关功能签名类型的有效性。
这是我的问题: 为什么sgi故意将void的情况专门化为返回类型?
template <class _Func,class _Ret>
struct _STL_GENERATOR_ERROR {
static _Ret __generator_requirement_violation (_Func& __f) {
return __f();
}
};
void hello() {}
int main(int argc,char const *argv[])
{
void (*ptr)() = &hello;
_STL_GENERATOR_ERROR<void(*)(),void>::__generator_requirement_violation(ptr);
return 0;
}
我的测试代码通常可以通过编译(clang / llvm / x86_64),并且可以正常运行。
如果我在理解原始代码或测试用例的设计时犯了错误,请随时指出!
大哥。
问题已解决,但是 跟进:为什么我的测试用例可以处理返回空类型的情况?
解决方法
尽管从ISO 98 1 开始,从void返回函数返回void表达式是合法的C ++,但我们可以想象某些早期的C ++编译器未实现该功能。
在这样的编译器上,通用模板将导致wch
= _Ret
的错误:
void
这就是为什么我们可以猜测是否添加了专业化功能(STL是 old ):
template <class _Func,class _Ret>
struct _STL_GENERATOR_ERROR {
static _Ret __generator_requirement_violation(_Func& __f) {
return __f();
}
};
1)
[stmt.return] / 3
表达式类型为“ cv-void” 的return语句只能在具有return type-of-cv-void 的函数中使用;该表达式将在函数返回其调用方之前进行求值。