MSVC C++11: non-standard constructor inheritance

元气小坏坏 提交于 2019-12-11 18:37:24

问题


In my Windows Native C++ library I've sometimes overdone it in terms of constructors without providing actual methods with same functionality (5-10 extra constructors). This makes basic extending a C++ class really hard as there's no proper constructor inheritance (without redeclaring them and forwarding calls).

I use MSVC. (Yes, smile all you want!) Question is: Is there a way to inherit constructors except the default constructor/copy-constructor using using? Because, if someone decided to extend a class where I abuse constructors (w/o adding new properties and with single inheritance), it's a nightmare.

(example code)

class parent {
public:
    parent(){
        std::cout << __FUNCTION__ << ':' << __LINE__ << std::endl;
    }
    // a non-standard constructor
    parent(const std::nullptr_t&):parent(){
        std::cout << __FUNCTION__ << ':' << __LINE__ << std::endl;
    }
};

// adds nothing to parent but helper methods
// and maybe self-reliant properties with their own constructors
// so no constructor is required to initialize child class properties
class child: public parent {
public:
     // using parent::parent; // of no real use
    child(){
        std::cout << __FUNCTION__ << ':' << __LINE__ << std::endl;
    }
    // need to forward call
    child(const std::nullptr_t&):parent(nullptr){
        std::cout << __FUNCTION__ << ':' << __LINE__ << std::endl;
    }
};

This multiple-constructor inheritance nightmare occurs especially when I build a my base CRTP class and need to extend it to build my default class and then allow others to extend and build their own variants while maintaining chainable method functionality (parent to child) in order.

In MSVC anyways, using parent::parent does not inherit the parent(const std::nullptr_t&). Is it supposed to according to the C++11 standard or not? Should I expect such functionality in VC13? This will greatly impact my choice in style for this public rewrite.

I also wonder why, for C++11, they did not figure out that when I'm extending a single class and not adding new properties, it should inherit the parent's default behavior (constructors, assignment operators, etc.). It just makes sense... and the compiler could figure it out.


回答1:


According to the VS2013 Roadmap from Herb Sutter's talk at //build, inheriting constructors are not coming to VC++ in the near future:

They are "planned."




回答2:


Delegating / inheriting ctors:

parent(const std::nullptr_t&):parent() { /* ... */

This is a delegating ctor [class.base.init]/6, whereas

using parent::parent;

inherits ctors from parent (all but copy/move ctors), according to the C++11 Standard [class.inhctor]

Question is: Is there a way to inherit constructors except the default copy-constructor using using?

Yes, for C++11. MSVC doesn't support that yet, and according to this MS site, VS2013 will support delegating ctors (but doesn't mention inheriting ctors).

I also wonder why, for C++11, they did not figure out that when I'm extending a single class and not adding new properties, it should inherit the parent's default behavior

No assumptions. Your derived class might need to be initialized differently. Inheriting ctors solve the problem with one line.


Is anything like version 3 even remotely possible?

I'm aware of that question, but I'm not that up-to-date with the latest drafts and proposals to be able to answer that for C++1y. For C++11, AFAIK, it's not possible - you can use macros to make it shorter (but not necessarily nicer):

#define EMPTY
#define DEF_MYCLASS_MEM(RET) template <typename tnChar> RET MyClass<tnChar>::
#define MYCLASS_TYPE typename MyClass<tnChar>

DEF_MYCLASS_MEM(EMPTY) MyClass() {}

DEF_MYCLASS_MEM(MYCLASS_TYPE::tString) Method1(const tString& aParam) const {
    return aParam;
}


来源:https://stackoverflow.com/questions/18063836/msvc-c11-non-standard-constructor-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!