C++使用模板的编译时间检查器

发布于 2024-10-19 05:00:12 字数 1271 浏览 1 评论 0原文

我有以下代码,取自现代 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃颗糖壮壮胆 2024-10-26 05:00:12

最令人烦恼的解析的另一个打击...

sizeof(CompileTimeChecker<(expr) != 0>((ERROR_##msg()))

与 Foo(Var)相同,

class Foo {};
class Bar {};
sizeof(Foo((Var()));

可以解释为一种类型(函数采用(不带参数的函数返回 Var)并返回 Foo),它是所以。

Yet another strike for the most vexing parse...

sizeof(CompileTimeChecker<(expr) != 0>((ERROR_##msg()))

Is the same as

class Foo {};
class Bar {};
sizeof(Foo((Var()));

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.

烟火散人牵绊 2024-10-26 05:00:12

就像 AProgrammer 指出的那样, (void)sizeof 并没有被编译器吞噬。我建议从 sizeof 中删除括号,如下所示:

(void)sizeof CompileTimeChecker<(expr) != 0>((ERROR_##msg()));\

这似乎让 g++ 接受它,并按照它可能的意思解释它。

如果 (void)sizeof 一直给您带来麻烦,您也可以在没有它的情况下获得静态检查功能,例如通过初始化 CompileTimeChecker 变量:

CompileTimeChecker<(expr) != 0> a((ERROR_##msg()));\

Like AProgrammer pointed out, the (void)sizeof isn't getting swallowed by the compiler. I suggest removing the parentheses from the sizeof, like this:

(void)sizeof CompileTimeChecker<(expr) != 0>((ERROR_##msg()));\

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:

CompileTimeChecker<(expr) != 0> a((ERROR_##msg()));\
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文