C++: nested class of a template class

后端 未结 4 2081
被撕碎了的回忆
被撕碎了的回忆 2020-12-10 13:09

Consider the following code:

template < typename T >
struct A
{
    struct B { };
};

template < typename T >
void f( typename A::B ) {          


        
4条回答
  •  攒了一身酷
    2020-12-10 13:24

    How can I avoid explicitly specifying int while calling f?

    Just make B declare its nesting class type

    template < typename T >
    struct A
    {
        struct B { typedef A outer; };
    };
    

    Then you can deduce it. The following takes the outer template, the inner's typedef and a return type

    template class Outer, typename D, typename R = void >
    struct nesting { };
    
    template class Outer, typename Arg, typename R>
    struct nesting< Outer, Outer, R > {
      typedef Arg arg1_type;
      typedef R type;
    };
    
    template < typename T >
    typename nesting::type
    f(T) { 
      /* nesting::arg1_type is A's T */ 
    }
    

提交回复
热议问题