Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Consider this C++ code :

template<typename Session>
class Step
{
public:
   using Session_ptr = boost::shared_ptr<Session>;
protected:
   Session_ptr m_session;
public:
   inline Step(Session_ptr session) : 
      m_session(session)
   {}

};

template<typename Socket>
class Session
{
public:
   Socket a;

   Session(Socket _a):
      a(_a)
   {}
};

template <typename Socket>
class StartSession : public Step<Session<Socket> >
{
protected:
   Session_ptr m_session; //Unknown type Session_ptr
public:
   inline StartSession(Session_ptr session) :
      Step<Session<Socket> >(session)
   {}

   void operator()(const boost::system::error_code& ec);
};

template <typename Socket>
class StartSession2 : public Step<Session<Socket> >
{
protected:
   typename Step<Session<Socket> >::Session_ptr m_session;
public:
   inline StartSession2(typename Step<Session<Socket> >::Session_ptr session) :
      Step<Session<Socket> >(session)
   {}

   void operator()(const boost::system::error_code& ec);
};

int main(int argc, char * argv[])
{
   Step<Session<int> >::Session_ptr b(new Session<int>(5)); //no problem
   StartSession<int >::Session_ptr bb(new Session<int>(5)); //gcc ok, clang refuses to remember the symbol since the class has errors
   StartSession2<int >::Session_ptr bbb(new Session<int>(5)); //no problem
   std::cout << b->a; // ok
   std::cout << bb->a; // gcc ok, clang bb not declared
   std::cout << bbb->a; // ok
   return 0;
}

As you can see, there are some strange (to me at least) things happening here...

First, why isn't Session_ptr accessible in the child classes ? I know because these are templated class, that make things more complicated... But I don't see any ambiguity here that makes the use of typename mandatory...

Then, why in the main, Session_ptr is accessible either as member of the base class either as member of a child class ?

like image 727
hl037_ Avatar asked Sep 05 '16 15:09

hl037_


2 Answers

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;
like image 73
Barry Avatar answered Oct 27 '22 19:10

Barry


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?

like image 22
marcinj Avatar answered Oct 27 '22 19:10

marcinj