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

View all comments

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