Overlapping template partial specialization when wanting an "override" case: how to avoid the error?

Posted by user173342 on Stack Overflow See other posts from Stack Overflow or by user173342
Published on 2012-12-13T16:43:32Z Indexed on 2012/12/13 17:03 UTC
Read the original article Hit count: 244

Filed under:
|
|

I'm dealing with a pretty simple template struct that has an enum value set by whether its 2 template parameters are the same type or not.

template<typename T, typename U> struct is_same { enum { value = 0 }; };
template<typename T> struct is_same<T, T> { enum { value = 1 }; };

This is part of a library (Eigen), so I can't alter this design without breaking it. When value == 0, a static assert aborts compilation.

So I have a special numerical templated class SpecialCase that can do ops with different specializations of itself. So I set up an override like this:

template<typename T> struct SpecialCase { ... };

template<typename LT, typename RT> struct is_same<SpecialCase<LT>, SpecialCase<RT>> { enum { value = 1 }; };

However, this throws the error:

more than one partial specialization matches the template argument list

Now, I understand why. It's the case where LT == RT, which steps on the toes of is_same<T, T>. What I don't know is how to keep my SpecialCase override and get rid of the error. Is there a trick to get around this?

edit: To clarify, I need all cases where LT != RT to also be considered the same (have value 1). Not just LT == RT.

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates