是否可以在编译时打印出C ++类的大小? [英] Is it possible to print out the size of a C++ class at compile-time?

查看:329
本文介绍了是否可以在编译时打印出C ++类的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在编译时确定C ++类的大小?

Is it possible to determine the size of a C++ class at compile-time?

我似乎记得一个模板元编程方法,但我可能会错误...

I seem to remember a template meta-programming method, but I could be mistaken...

对不清楚 - 我想要的大小打印在构建输出窗口

sorry for not being clearer - I want the size to be printed in the build output window

推荐答案

如果真的需要在编译器输出中获取sizeof(X),可以将其用作不完整模板类型的参数:

If you really need to to get sizeof(X) in the compiler output, you can use it as a parameter for an incomplete template type:

template<int s> struct Wow;
struct foo {
    int a,b;
};
Wow<sizeof(foo)> wow;

$ g++ -c test.cpp
test.cpp:5: error: aggregate ‘Wow<8> wow’ has incomplete type and cannot be defined

这篇关于是否可以在编译时打印出C ++类的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆