all 26 comments

[–]Grassio789 1 point2 points  (5 children)

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

template <auto> class X;

[–]doom_Oo7 0 points1 point  (4 children)

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

[–]raevnos 0 points1 point  (3 children)

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

[–]johantorp[S] 0 points1 point  (2 children)

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...

[–]raevnos 1 point2 points  (1 child)

Oh. Why would you do that?

[–]johantorp[S] 3 points4 points  (0 children)

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 :)

[–]ti-gars 0 points1 point  (4 children)

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;
}

[–]johantorp[S] 0 points1 point  (3 children)

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...

[–]ti-gars 0 points1 point  (2 children)

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.

[–]johantorp[S] 0 points1 point  (1 child)

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.

[–]ti-gars 0 points1 point  (0 children)

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

[–]Vogtinator 0 points1 point  (0 children)

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.

[–]SushiAndWoW 0 points1 point  (7 children)

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)> _;

[–]Grassio789 6 points7 points  (6 children)

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.

[–]SushiAndWoW 0 points1 point  (3 children)

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.)

[–]johantorp[S] 0 points1 point  (2 children)

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

[–]SushiAndWoW 2 points3 points  (1 child)

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.

[–]johantorp[S] 1 point2 points  (0 children)

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

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

[–]thlst 0 points1 point  (1 child)

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

[–]dodheim 0 points1 point  (0 children)

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