r/cpp • • Oct 20 '16

Compile time sizeof()

http://johantorp.com/tidbits/index.php/2016/10/14/compile-time-sizeof-alignof/
0 Upvotes

26 comments sorted by

1

u/raevnos Oct 20 '16

Does it work if you actually define the class, not just declare it exists?

1

u/johantorp Oct 20 '16 edited Oct 20 '16

No. The point is that you deliberately force a compile error that prints the sizeof() result. I guess I should have made this more obvious in the post...

2

u/raevnos Oct 20 '16

Oh. Why would you do that?

4

u/johantorp Oct 20 '16

To quickly get the size or alignment of some type without having to open up the header file and look at the definition. And to not have to sum up member sizes and follow inheritance chains manually.

SushiAndWow pointed out you can get sizeof from IntelliSense in Visual Studio. Intellisense is unfortunately still unacceptably slow for my use cases. Also, I often code in a text editor and build from commandline and then IntelliSense isn't of much help. This trick is IDE-independent and works across different compilers.

I tried to make the post a bit fun but probably failed. Most people didn't get it and many down-voted this post, I presume since the example didn't compile. Fail :)

1

u/ti-gars Oct 24 '16

I prefer the switch trick, worked with all compilers I tried, not like the template<int> trick...

struct X { .. };
int i;
switch(i)
{
case sizeof(X):
case sizeof(X): // outputs an error with the size of being used twice
break;
}

1

u/johantorp Oct 24 '16

Interesting, thanks for sharing.

Which compiler does this work on where template<int> struct doesn't? The template<int> trick works on gcc, clang and MSVC, which are all the compilers I care about :) template<int> does not work on Intel's compiler but neither does your sizeof version..

Also, the sizeof version is significantly more verbose and thus cumbersome to use...

1

u/ti-gars Oct 24 '16

there's been versions (don't remember which ones) of both clang and msvc that were not outputing the sizeof size :-/ made me switch at that point.

1

u/johantorp Oct 24 '16

Ah, I think you were using function templates then. They only worked on one compiler IIRC, can't recall which. class/struct templates has always worked on all three as far as I can remember.

1

u/ti-gars Oct 24 '16

nope, definitely the same struct trick :-) anyway nice trick!

2

u/Grassio789 Oct 20 '16

How about C++17 auto template parameter? Like:

template <auto> class X;

1

u/doom_Oo7 Oct 20 '16

are there compilers that support this yet ? I tried in gcc 7 git and clang 3.9 and they both don't accept it.

0

u/[deleted] Oct 20 '16

[deleted]

1

u/doom_Oo7 Oct 20 '16

hmm.. I don't understand, your link does not compile :

prog.cc:4:5: error: implicit instantiation of undefined template 'S<4>'
    S<sizeof(int)>{};
    ^
prog.cc:1:23: note: template is declared here
template<auto> struct S;
                      ^
1 error generated.

3

u/thlst Oct 20 '16

... That's exactly the point.

2

u/doom_Oo7 Oct 20 '16

ah yes, sorry, I thought that it was about the template<auto> feature not compile time sizeof.

1

u/SushiAndWoW Oct 20 '16

Umm... The problem is not sizeof, it's that the class actually is not defined. You only declare it.

Try this:

template<int> class X {};
X<sizeof(SomeType)> _;

8

u/Grassio789 Oct 20 '16

I think making it fails compilation and displays the output in the error message is the whole idea. Similar to the "Type Displayer" trick mentioned by Scott Meyers in CppCon 2014.

1

u/SushiAndWoW Oct 20 '16

Oh, I see. Nifty.

In VS 2015, there's usually no need. You can just hover over sizeof(...), and it shows the value in a tooltip. (Also appropriately reflects changes if you e.g. change build configuration.)

1

u/johantorp Oct 20 '16

That doesn't work for me, maybe because I have IntelliSense disabled.

3

u/SushiAndWoW Oct 20 '16 edited Oct 20 '16

Indeed, that would be one way for IntelliSense to not work. :)

I tested this on a huge solution, BTW – over 100 mostly C++ projects – and the sizeof tooltip became available as soon as the source files were parsed, which was a reasonable time (e.g. 10s of seconds) given the size of the solution.

On a small project, of course, it would be practically immediate.

2

u/johantorp Oct 20 '16

I updated the post with info about hovering over size-of expressions.

Thanks for the feedback, this was news to me :)

1

u/thlst Oct 20 '16

It's also mentioned in the Effective Modern C++ book.

1

u/dodheim Oct 22 '16

And in Abrahams and Gurtovoy's C++ Template Metaprogramming book.

1

u/Vogtinator Oct 24 '16

KDevelop has this implemented directly, when hovering over a struct it prints the size of the struct in bytes and also the offsets of struct members.

0

u/[deleted] Oct 21 '16

[deleted]

1

u/johantorp Oct 21 '16

int, because you want to type as few characters as possible.

Why would you compile your whole solution? Just compile the .cpp you happen to be working in atm. If you already have compilation errors in it put this on top of the file or just use some other random small .cpp file that knows about the type and compiles quickly.

0

u/[deleted] Oct 21 '16

[deleted]

1

u/johantorp Oct 21 '16

That requires you to guess the size

0

u/[deleted] Oct 21 '16

[deleted]

2

u/johantorp Oct 21 '16

The whole point is to not do it manually - not having to go to the header file and find all members or worse have to follow a deep inheritance chain across lots of headers.

Should've been more clear about that, I'm a complete blogging n00b. At least I learned one important thing from this post - be extremely specific about what problem is being solved :)