Conflict between copy constructor and forwarding constructor

前端 未结 3 460
面向向阳花
面向向阳花 2020-12-01 19:03

This problem is based on code that works for me on GCC-4.6 but not for another user with CLang-3.0, both in C++0x mode.

template 
struct My         


        
3条回答
  •  离开以前
    2020-12-01 19:39

    I upvoted Dietmar's answer because I totally agree with him. But I want to share a "solution" I was using some time earlier to avoid these issues:

    I intentionally added a dummy parameter to the variadic constructor:

    enum fwd_t {fwd};
    
    template
    class wrapper
    {
        T m;
    public:
        template
        wrapper(fwd_t, Args&&...args)
        : m(std::forward(args)...)
        {}
    };
    
    :::
    
    int main()
    {
        wrapper w (fwd,"hello world");
    }
    

    Especially since the constructor would accept anything without this dummy parameter, it seems appropriate to make user code explicitly choose the correct constructor by (sort of) "naming" it.

    It might not be possible in your case. But sometimes you can get away with it.

提交回复
热议问题