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

hl037_

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 ?

Barry

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;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

I can "pickle local objects" if I use a derived class?

来自分类Dev

Call derived class method from base class instance

来自分类Dev

Static Instance Base/Derived class

来自分类Dev

Why an instance of inherited class can't access to protected member of base class in different package

来自分类Dev

c++ - converting a base class pointer to a derived class pointer

来自分类Dev

Why I can instantiate a static inner class?

来自分类Dev

How can I copy a templated instance with a virtual base class?

来自分类Dev

Using Castle.Windsor to register an interceptor for only the derived class, not the base class

来自分类Dev

C# Inheritance Prevent Base Class Method Call Derived Class method

来自分类Dev

Why can't I use multiple selection with listbox in zk framework?

来自分类Dev

dart, why can't I use generics on a method

来自分类Dev

Why I can't reach form properties from another file?

来自分类Dev

Can I create a class which can be unpacked?

来自分类Dev

使用Alias = Class; 与泛型

来自分类Dev

How can I define an UUID for a class, and use __uuidof, in the same way for g++ and Visual C++?

来自分类Dev

Why is one of my base class constructors deleted? (C++11)

来自分类Dev

Can I pass a Class type as a procedure parameter

来自分类Dev

Can I create a class [] operator in Typescript

来自分类Dev

Can I Reference Const Fields From a Nested Struct Without Referencing the Containing Class?

来自分类Dev

c++ derived class with private access specifier

来自分类Dev

Disable method override in template derived class

来自分类Dev

boost.python: Calling function of a base class from Python

来自分类Dev

Syntax with templates in class functions with a binary search tree

来自分类Dev

可以撤销class_alias()吗?

来自分类Dev

PHP class_alias返回错误

来自分类Dev

Can I / Should I serialize a constant variable in a C# class?

来自分类Dev

Why can't I split this python list?

来自分类Dev

Why can't I create a new Color?

来自分类Dev

Why can't I add a control the page's header from the an inline ASPX code nugget?

Related 相关文章

  1. 1

    I can "pickle local objects" if I use a derived class?

  2. 2

    Call derived class method from base class instance

  3. 3

    Static Instance Base/Derived class

  4. 4

    Why an instance of inherited class can't access to protected member of base class in different package

  5. 5

    c++ - converting a base class pointer to a derived class pointer

  6. 6

    Why I can instantiate a static inner class?

  7. 7

    How can I copy a templated instance with a virtual base class?

  8. 8

    Using Castle.Windsor to register an interceptor for only the derived class, not the base class

  9. 9

    C# Inheritance Prevent Base Class Method Call Derived Class method

  10. 10

    Why can't I use multiple selection with listbox in zk framework?

  11. 11

    dart, why can't I use generics on a method

  12. 12

    Why I can't reach form properties from another file?

  13. 13

    Can I create a class which can be unpacked?

  14. 14

    使用Alias = Class; 与泛型

  15. 15

    How can I define an UUID for a class, and use __uuidof, in the same way for g++ and Visual C++?

  16. 16

    Why is one of my base class constructors deleted? (C++11)

  17. 17

    Can I pass a Class type as a procedure parameter

  18. 18

    Can I create a class [] operator in Typescript

  19. 19

    Can I Reference Const Fields From a Nested Struct Without Referencing the Containing Class?

  20. 20

    c++ derived class with private access specifier

  21. 21

    Disable method override in template derived class

  22. 22

    boost.python: Calling function of a base class from Python

  23. 23

    Syntax with templates in class functions with a binary search tree

  24. 24

    可以撤销class_alias()吗?

  25. 25

    PHP class_alias返回错误

  26. 26

    Can I / Should I serialize a constant variable in a C# class?

  27. 27

    Why can't I split this python list?

  28. 28

    Why can't I create a new Color?

  29. 29

    Why can't I add a control the page's header from the an inline ASPX code nugget?

热门标签

归档