brahmin
brahmin

Reputation: 588

Template structure specialization using typename ::

I want to specialize struct B with a type which is defined inside another struct A.

One can see a MWE down in the post.

First I'm surprised the two specializations of B can exist together. I added the second one to show the problem. If removed, code see no proper specialization for B. I'd prefer to only keep the first specialization.

So, my questions are :

definitions.h :

template <class T>
class X {};

template <class T>
class Xitem {};


template <class T>
struct A;

template <class T>
struct A< X<T> > {
    typedef Xitem<T> Titem;
};

template <class T>
struct B;

template <class T>//I need this one only
struct B< typename A< X<T> >::Titem > {
    static void foo() {
        std::cout << "foo 1" << std::endl;
    }
};

template <class T>
struct B< Xitem<T> > {
    static void foo() {
        std::cout << "foo 2" << std::endl;
    }
};

main.cpp :

#include "definitions.h"

int main(int argc, char *argv[]) {

    B< A< X<int> >::Titem >::foo();
}

Output : "foo 2" Using Xcode 7

Upvotes: 1

Views: 233

Answers (1)

brahmin
brahmin

Reputation: 588

Answer given in comments by Igor Tandetnik :

Imagine that you use, say, B. You appear to expect the compiler to instantiate A> for every possible type T, in the hopes that there might exist a specialization that happens to have a member typedef int Titem (or to prove that there is none such). Or else to engage in a theorem-proving exercise in order to prove that, with currently visible definitions and specializations of A, no possible T may result in A< X >::Titem being an int. The compiler does not go to such lengths; it instead declares T to be non-deducible context, and never uses this specialization.

Upvotes: 1

Related Questions