Template partial specialization

后端 未结 3 1835
不知归路
不知归路 2020-12-11 08:47

Would any one knows according to what rules code below doesn\'t compile?

template 
struct B
{
    typedef T type;
};

template

        
3条回答
  •  爱一瞬间的悲伤
    2020-12-11 09:00

    How do you think that will work? The compiler will look to see if there is a class T somewhere that has a typedef "type" to your class?

    It just won't. Even though it's a pointer.

    Remember that presumably your B template is presumably specialised in places so that type is not always T*, but it can't deduce it with reverse engineering.

    For those who did not understand my answer fully, what you are asking the compiler to do is find a class U such that B::type is the class you pass in as a parameter.

    class Foo;
    class Bar;
    
    template<> struct B
    {
      typedef int type;
    };
    
    template<> struct B
    {
      typedef int type;
    };
    
    X // ambiguous, T is Foo or Bar?
    

    It is difficult to know exactly why you are trying to do what you are. You can do a partial specialization on all pointers and then a total specialization on specific pointers, which could be implement in terms of another template.

提交回复
热议问题