Forwarding all constructors in C++0x

后端 未结 2 1199
逝去的感伤
逝去的感伤 2020-11-30 04:32

What is the correct way to forward all of the parent\'s constructors in C++0x?

I have been doing this:

class X: public Super {
    template

        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 05:01

    There is a better way in C++0x for this

    class X: public Super {
      using Super::Super;
    };
    

    If you declare a perfect-forwarding template, your type will behave badly in overload resolution. Imagine your base class is convertible from int and there exist two functions to print out classes

    class Base {
    public:
      Base(int n);
    };
    
    class Specific: public Base {
    public:
      template
        Specific(Args&&... args);
    };
    
    void printOut(Specific const& b);
    void printOut(std::string const& s);
    

    You call it with

    printOut("hello");
    

    What will be called? It's ambiguous, because Specific can convert any argument, including character arrays. It does so without regard of existing base class constructors. Inheriting constructors with using declarations only declare the constructors that are needed to make this work.

提交回复
热议问题