On Friday, 5 August 2016 16:25:24 UTC+3, Daniel wrote:
> Consider
>
> class U
> {
> };
>
> class V
> {
> };
>
> class X
> {
> public:
> typedef U u_t;
> };
>
> class Y
> {
> public:
> typedef U u_t;
> typedef V v_t;
> };
>
> How to distinguish trait class specializations for classes X and Y
> (when using the default isn't an option)?
When instantiating a default (not specialization) is error then declare it
but don't define or put 'static_assert' in it.
>
> // primary template
> template<class T, class Enable = void>
> struct A {
> A()
> {
> std::cout << "default" << std::endl;
> }
> };
>
> // Matches Y
> template <class T>
> class A<T,typename std::enable_if<std::is_same<typename T::u_t,U>::value &&
> std::is_same<typename T::v_t,V>::value>::type> {
> public:
> A()
> {
> std::cout << "has u_t and v_t" << std::endl;
> }
> };
>
> // How to fix this to match just X?
> template <class T>
> class A<T,typename std::enable_if<std::is_same<typename T::u_t,U>::value>::type> {
> public:
> A()
> {
> std::cout << "has u_t but not v_t" << std::endl;
> }
> };
That is simple:
template <class T>
class A<T,typename std::enable_if<std::is_same<T,X>::value>::type> {
Fixed? That implements what comment tells (enables to match just X).
If that actually was not it then I am no psychic so I don't know what conditions you actually had in mind. On any case you can use all inbuilt
operators (like ! or ||) in that 'enable_if' to express exact set of
conditions that you want to enable since all of those are 'constexpr'.
One library in what lot of things you seem to be interested in are
implemented in generic manner is Boost Type Traits Introspection (or may
be called "Boost.TTI", I'm not sure). So it, its code or its documentation
can save you some time or at least give valuable insights.