Make C++ fail compilation on specific instantiation of template function

Viewed 2580

I'm working on a project which has an template function as so:

template <class T> 
T foo<T>(T val) { return someFunc(val); }

template <>
bool foo<bool>(bool val) { return otherFunc(val); };

Now, I have a class Bar, which I don't want to accept as input. In fact, I want it to generate an easy to spot compile error. The problem is that if I do this:

template <>
Bar foo<Bar>(Bar val) { static_assert(false,"uh oh..."); }

It fails on every compile. I found https://stackoverflow.com/a/3926854/7673414, which says that I need to make reference to the template type, otherwise the static assert always takes place. The problem is I don't have a template type here. If I do:

template< typename T >
struct always_false { 
    enum { value = false };  
};

template <>
Bar foo<Bar>(Bar val) { static_assert(always_false<Bar>::value,"uh oh..."); }

then it also always fails compiling. Is there a way to ensure that an instantiation of the template with type Bar always causes a compile error?

4 Answers
Related