Search code examples
c++templatescompiler-errorspartial-specialization

Force compile time error when specialized template function is invoked


I have a template function. It has well defined semantics so long as the argument is not a pointer type. If someone calls this function passing an argument of type pointer I want to force a compile time error. I have no trouble writing the general (legal) template and the corresponding partially specialized (illegal) version. I just cannot figure out how to defer an error from function definition to function invocation.


Solution

  • You don't need to specialize it, actually. Just add this in your function body:

    BOOST_STATIC_ASSERT(!boost::is_pointer<T>()::value);
    

    This will cause a failure which should be fairly easy to understand.