Why can't I use alias from a base class in a derived class with templates?

后端 未结 3 1871
Happy的楠姐
Happy的楠姐 2021-01-20 23:37

Consider this C++ code :

template
class Step
{
public:
   using Session_ptr = boost::shared_ptr;
protected:
   Session_pt         


        
相关标签:
3条回答
  • 2021-01-21 00:16

    This is because in StartSession class your Session_ptr type is seen as non dependent name, so it is being looked up with out instatiation of base class which is dependent. This is why you need to make reference to this name dependent some how, for example by qualifying it in a way as g++ suggests in warnings:

    note: (perhaps 'typename Step<Session<Socket> >::Session_ptr' was intended)
    

    btw. some compilers like Visual Studio (I checked it with 2015 now) will happily compile your code. This is because VS does not implement properly a two-phase template instantiation. See here for more on that: What exactly is "broken" with Microsoft Visual C++'s two-phase template instantiation?

    0 讨论(0)
  • 2021-01-21 00:22

    Unqualified lookup does not look in dependent base classes in class templates.

    So here:

    template <typename Socket>
    class StartSession : public Step<Session<Socket> >
    {
    protected:
       Session_ptr m_session; // <== unqualified name lookup on Session_ptr
       // ...
    };
    

    Step<Session<Socket>> is a dependent base class of StartSession<Socket>. In order to lookup there, you'll have to do qualified name lookup (which is what you're doing in StartSession2):

    template <typename Socket>
    class StartSession : public Step<Session<Socket> >
    {
    protected:
       typename Step<Session<Socket>>::Session_ptr m_session;
       // ...
    };
    

    Or simply add the alias yourself:

    using Session_ptr = typename Step<Session<Socket>>::Session_ptr;
    
    0 讨论(0)
  • 2021-01-21 00:36

    Here I give another example with the solutions already given in other answers presented.

    std::iterator<std::input_iterator_tag, MyType> is a dependent base class of Myterator<MyType>. In order to lookup there, you'll have to do qualified name lookup.

    // std::iterator example  from http://www.cplusplus.com/reference/iterator/iterator/
    //***************************************************************************************
    #include <iostream>     // std::cout
    #include <iterator>     // std::iterator, std::input_iterator_tag
    
    template <class MyType>
    class MyIterator : public std::iterator<std::input_iterator_tag, MyType>
    {
      // The working alternatives, one per row
      //typename std::iterator<std::input_iterator_tag, MyType>::pointer  p;
      //using pointer = typename std::iterator<std::input_iterator_tag, MyType>::pointer; pointer p;
      //using typename std::iterator<std::input_iterator_tag, MyType>::pointer; pointer p;
      //using Iter = typename std::iterator<std::input_iterator_tag, MyType>; typename Iter::pointer p;
      pointer p; // This does not work while any of alternatives in comments above do work
    public:
      MyIterator(MyType* x) :p(x) {}
      MyType& operator*() {return *p;}
    
    };
    
    int main () {
      int numbers[]={10,20,30,40,50};
      MyIterator<int> from(numbers);
      std::cout << *from << ' ';
      std::cout << '\n';
    
      return 0;
    }
    
    0 讨论(0)
提交回复
热议问题