How can I prevent a variadic constructor from being preferred to the copy constructor?

前端 未结 4 1831
生来不讨喜
生来不讨喜 2020-12-13 14:09

I have a template \'Foo\', which owns a T, and I\'d like it to have a variadic constructor that forwards its arguments to T\'s constructor:

template

        
4条回答
  •  悲&欢浪女
    2020-12-13 14:37

    Disable the constructor when the argument type is the same type as or derived from this:

    template
    struct is_this_or_derived : public std::false_type {};
    
    template
    struct is_this_or_derived
        : public std::is_base_of, std::decay_t >::type {};
    
    template
    using disable_for_this_and_derived 
          = std::enable_if_t::value>;
    

    Use it as

    template >
                                                    //^^^^
                                                    //this needs to be adjusted for each class
    Foo(Args&&... args) : t(std::forward(args)...) {}
    

提交回复
热议问题