If the function that performs these checks just does those conversions that don't have any side effects, the optimizer will likely optimize that entire code away and will not generate any code for the function body. The only thing left will be the function symbol and a return instruction.
As it turns out (tested with GCC 4.5.1) not even the function's symbol need to be emitted. The compiler optimized out the address-taking, and then observed no other code in that file accesses the function, not emitting code for it. I think that's OK, because any other translation unit that needs that definition provides a definition by itself - so they don't depend on other translation units's compilation anyway.
Notice that using this method, the checks only trigger once you create an object of Container<T>
. Otherwise, the constructor of Derived_from
will never be implicitly instantiated and the checks are never done.
There are ways that completely get this done without any such dummy code, like boost::is_base_of
.