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

前端 未结 3 1999
暖寄归人
暖寄归人 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:37

    You could use a helper function that creates the corresponding type:

    template 
    Box make_boxed(const R& value){
        return Box(value);
    }
    

    it may seem like additional complication that one has to specify the T, on the other hand you can use auto for the returned type. Complete example:

    #include 
    
    template
    class Box {
    public:
        Box(const T& value) : _value(value) {};
    private:
        T _value;
        /* ... */
    };
    
    typedef Box String;
    
    int main(int argc, char* argv[]) {
        auto a = make_boxed("asd");
    }
    

提交回复
热议问题