Does inheriting constructors work with templates in C++0x?

前端 未结 2 651
灰色年华
灰色年华 2020-12-29 04:33

In C++0x, you can use the using keyword to inherit constructors, like so:

class B { B(int) {} };

class A : public B { using B::B; };

2条回答
  •  星月不相逢
    2020-12-29 05:09

    Yes, it appears it does, from the standard (Feb 2011 Draft), section 12.9:

    template< class T >
    struct D : T {
    using T::T; // declares all constructors from class T
    ~D() { std::clog << "Destroying wrapper" << std::endl; }
    };
    

    Class template D wraps any class and forwards all of its constructors, while writing a message to the standard log whenever an object of class D is destroyed. —end example

    Another thing to note, while the standard allows it, according to this list, only 1 compiler, IBM XLC++, supports this feature in a release version. GCC only currently supports it with a patch.

    Edit: AJG85 pointed out that the T in the template always refers to the placeholder, so the 'using T::T' always refers to the template argument.

提交回复
热议问题