Can't assign string literal to boxed std::string vector

前端 未结 3 2010
暖寄归人
暖寄归人 2021-02-13 16:17

This is a simplified version of my type system:

#include 
#include 

template
class Box {
public:
    Box(const T&a         


        
3条回答
  •  借酒劲吻你
    2021-02-13 16:50

    c currently need 2 implicit user conversions (const char [N] -> std::string -> String) whereas only one is allowed.

    You may add template constructor to Box

    template
    class Box {
    public:
        Box() = default;
        Box(const Box&) = default;
        Box(Box&&) default;
        ~Box() = default;
    
        Box& operator=(const Box&) = default;
        Box& operator=(Box&&) = default;
    
        template ::value
                                   && (!std::is_same>::value
                                      || sizeof...(Us) != 0)>* = nullptr>
        Box(U0&& u0, Us&&... us) : _value(std::forward(u0), std::forward(us)...) {}
    private:
        T _value;
        /* ... */
    };
    

    Demo Demo2

提交回复
热议问题