C++使用模板的编译时间检查器
我有以下代码,取自现代 C++ 设计。当我使用它时,我收到编译错误,我认为操作数的大小无效。任何人都可以指出问题是什么。谢谢!
template<bool>
struct CompileTimeChecker {
CompileTimeChecker(...);
};
template<>
struct CompileTimeChecker<false> {
};
#define STATIC_CHECK(expr, msg) \
{\
class ERROR_##msg {}; \
(void)sizeof(CompileTimeChecker<(expr) != 0>((ERROR_##msg())));\
}
template <class To, class From>
To safe_reinterpret_cast(From from) {
STATIC_CHECK(sizeof(From) <= sizeof(To), Destination_Type_Too_Narrow);
return reinterpret_cast<To>(from);
}
int main(void)
{
int a[20];
void* somePointer = a;
char c = safe_reinterpret_cast<int>(somePointer);
}
错误:
d:\technical\c++study\readparsing\readparsing\addressconv.cpp(29) : 错误 C2066: 转换为函数类型是非法的 1> d:\technical\c++study\readparsing\readparsing\addressconv.cpp(37) :请参阅正在编译的函数模板实例化“To safe_reinterpret_cast(From)”的参考 1>和 1> [ 1>至=整数, 1>来自=无效* 1> ] 1>d:\technical\c++study\readparsing\readparsing\addressconv.cpp(29) : 错误 C2070: 'CompileTimeChecker<__formal>; (safe_reinterpret_cast::ERROR_Destination_Type_Too_Narrow (__cdecl *)(void))':操作数大小非法 1>和 1> [ 1> __正式=真 1> ]
I am having following code which is taken from modern C++ design. While i am using it i am getting compiation error i think invalid sizeof opearand. Can any one point out what is the problem. Thanks!
template<bool>
struct CompileTimeChecker {
CompileTimeChecker(...);
};
template<>
struct CompileTimeChecker<false> {
};
#define STATIC_CHECK(expr, msg) \
{\
class ERROR_##msg {}; \
(void)sizeof(CompileTimeChecker<(expr) != 0>((ERROR_##msg())));\
}
template <class To, class From>
To safe_reinterpret_cast(From from) {
STATIC_CHECK(sizeof(From) <= sizeof(To), Destination_Type_Too_Narrow);
return reinterpret_cast<To>(from);
}
int main(void)
{
int a[20];
void* somePointer = a;
char c = safe_reinterpret_cast<int>(somePointer);
}
Error:
d:\technical\c++study\readparsing\readparsing\addressconv.cpp(29) : error C2066: cast to function type is illegal
1> d:\technical\c++study\readparsing\readparsing\addressconv.cpp(37) : see reference to function template instantiation 'To safe_reinterpret_cast(From)' being compiled
1> with
1> [
1> To=int,
1> From=void *
1> ]
1>d:\technical\c++study\readparsing\readparsing\addressconv.cpp(29) : error C2070: 'CompileTimeChecker<__formal> (safe_reinterpret_cast::ERROR_Destination_Type_Too_Narrow (__cdecl *)(void))': illegal sizeof operand
1> with
1> [
1> __formal=true
1> ]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最令人烦恼的解析的另一个打击...
与 Foo(Var)相同,
可以解释为一种类型(函数采用(不带参数的函数返回 Var)并返回 Foo),它是所以。
Yet another strike for the most vexing parse...
Is the same as
and as Foo(Var) can be interpreted either as a type (function taking a (function without an argument an returning a Var) and returning a Foo), it is so.
就像 AProgrammer 指出的那样, (void)sizeof 并没有被编译器吞噬。我建议从 sizeof 中删除括号,如下所示:
这似乎让 g++ 接受它,并按照它可能的意思解释它。
如果 (void)sizeof 一直给您带来麻烦,您也可以在没有它的情况下获得静态检查功能,例如通过初始化 CompileTimeChecker 变量:
Like AProgrammer pointed out, the (void)sizeof isn't getting swallowed by the compiler. I suggest removing the parentheses from the sizeof, like this:
That seems to make g++ accept it, and interpret it the way it was probably meant to.
If that (void)sizeof keeps giving you trouble, you can get the static checking functionality without it too, for example by initializing a CompileTimeChecker variable: