In C++0x, I can do something like this:
double f(double x) { return x; }
template<class T>
T f(T x) = delete;
To prevent f() from being called on any other type than double.
What I’m trying to do is similar, however, not quite the same.
I have a function that operates on pointer arrays. For example:
template<class T>
T* some_string_function(T* x);
I want to be able to make T work for char, char16_t, and char32_t, but not any other type. I was thinking that C++0x’s delete would be a good way to accomplish this. Basically, I want to be able to prevent this function from working with any type that isn’t one of the three Unicode char types, but I still want to have the benefits of function templates, which allow me to generalise types and avoid repeating code.
What would be the best way to solve this problem? Is it possible?
Use
boost::enable_if, along with type traits.(assuming
is_char_typeis a type trait you define, which evaluates to true for the desired types, and false for all others)