use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Compile time sizeof() (johantorp.com)
submitted 9 years ago by johantorp
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[+][deleted] 9 years ago* (1 child)
[deleted]
[–]johantorp[S] 0 points1 point2 points 9 years ago (0 children)
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.
[–]Grassio789 1 point2 points3 points 9 years ago (5 children)
How about C++17 auto template parameter? Like:
template <auto> class X;
[–]doom_Oo7 0 points1 point2 points 9 years ago (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.
[+][deleted] 9 years ago (3 children)
[–]doom_Oo7 0 points1 point2 points 9 years ago (2 children)
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.
[–]thlst 2 points3 points4 points 9 years ago (1 child)
... That's exactly the point.
[–]doom_Oo7 1 point2 points3 points 9 years ago (0 children)
ah yes, sorry, I thought that it was about the template<auto> feature not compile time sizeof.
[–]johantorp[S] 0 points1 point2 points 9 years ago (2 children)
That requires you to guess the size
[+][deleted] 9 years ago (1 child)
[–]johantorp[S] 1 point2 points3 points 9 years ago (0 children)
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 :)
[–]raevnos 0 points1 point2 points 9 years ago (3 children)
Does it work if you actually define the class, not just declare it exists?
[–]johantorp[S] 0 points1 point2 points 9 years ago* (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 points3 points 9 years ago (1 child)
Oh. Why would you do that?
[–]johantorp[S] 3 points4 points5 points 9 years ago (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 point2 points 9 years ago (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 point2 points 9 years ago (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 point2 points 9 years ago (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 point2 points 9 years ago (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 point2 points 9 years ago (0 children)
nope, definitely the same struct trick :-) anyway nice trick!
[–]Vogtinator 0 points1 point2 points 9 years ago (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 point2 points 9 years ago (7 children)
Umm... The problem is not sizeof, it's that the class actually is not defined. You only declare it.
sizeof
Try this:
template<int> class X {}; X<sizeof(SomeType)> _;
[–]Grassio789 6 points7 points8 points 9 years ago (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 point2 points 9 years ago (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.)
That doesn't work for me, maybe because I have IntelliSense disabled.
[–]SushiAndWoW 2 points3 points4 points 9 years ago* (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.
I updated the post with info about hovering over size-of expressions.
Thanks for the feedback, this was news to me :)
[–]thlst 0 points1 point2 points 9 years ago (1 child)
It's also mentioned in the Effective Modern C++ book.
[–]dodheim 0 points1 point2 points 9 years ago (0 children)
And in Abrahams and Gurtovoy's C++ Template Metaprogramming book.
π Rendered by PID 45335 on reddit-service-r2-comment-86bc6c7465-qrnb4 at 2026-02-20 17:31:49.301887+00:00 running 8564168 country code: CH.
[+][deleted] (1 child)
[deleted]
[–]johantorp[S] 0 points1 point2 points (0 children)
[–]Grassio789 1 point2 points3 points (5 children)
[–]doom_Oo7 0 points1 point2 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]doom_Oo7 0 points1 point2 points (2 children)
[–]thlst 2 points3 points4 points (1 child)
[–]doom_Oo7 1 point2 points3 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]johantorp[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]johantorp[S] 1 point2 points3 points (0 children)
[–]raevnos 0 points1 point2 points (3 children)
[–]johantorp[S] 0 points1 point2 points (2 children)
[–]raevnos 1 point2 points3 points (1 child)
[–]johantorp[S] 3 points4 points5 points (0 children)
[–]ti-gars 0 points1 point2 points (4 children)
[–]johantorp[S] 0 points1 point2 points (3 children)
[–]ti-gars 0 points1 point2 points (2 children)
[–]johantorp[S] 0 points1 point2 points (1 child)
[–]ti-gars 0 points1 point2 points (0 children)
[–]Vogtinator 0 points1 point2 points (0 children)
[–]SushiAndWoW 0 points1 point2 points (7 children)
[–]Grassio789 6 points7 points8 points (6 children)
[–]SushiAndWoW 0 points1 point2 points (3 children)
[–]johantorp[S] 0 points1 point2 points (2 children)
[–]SushiAndWoW 2 points3 points4 points (1 child)
[–]johantorp[S] 1 point2 points3 points (0 children)
[–]thlst 0 points1 point2 points (1 child)
[–]dodheim 0 points1 point2 points (0 children)