3 ms·
The sizeof() forces instantiation of the template. The trick is that the template declares a friend function with a body. Being a "friend" declaration, it is n
by kentonv 5y ago
The sizeof() forces instantiation of the template.
The trick is that the template declares a friend function with a body. Being a "friend" declaration, it is not scoped within the template. However, the body is defined within the template and is allowed to use the template's parameters.
The result is that the friend function's definition depends on how the template was instantiated, even though the friend function is not scoped within the template. If you instantiate the template two different ways, the compiler actually complains that the friend function has two different definitions.
What this all means is that you can write some code that instantiates a template, and then later on, you can observe how the template was instantiated. Instantiating a template has a programmatically-observable side effect.
And you can use that to trick the compiler into giving you more information about other declarations. SFINAE on steroids basically.
- hoseja 5y agoI hate you need to trick the compiler. Reflection can't come soon enough.