Forwarding all constructors in C++0x

后端 未结 2 1194
逝去的感伤
逝去的感伤 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 04:41

    I think you need to do Super(std::forward(args)...) if you want things forwarded properly. args has a name, so I believe it will bind to regular references before rvalue references.

    More importantly, though, you won't be forwarding copy constructors this way. The compiler will still generate one for you, because it doesn't consider template constructors. In the example provided, you're okay, because the compiler generated one will just call the parent copy constructor anyhow, which is exactly what you want. If that's not appropriate in a more complicated case, then you'll have to write it yourself.

提交回复
热议问题