C++ 编译错误,对成员变量多重赋值的方法,模板类C++;

C++ 编译错误,对成员变量多重赋值的方法,模板类C++;,c++,c++11,visual-c++,c++14,C++,C++11,Visual C++,C++14,问题方法: template <typename T> void SerializableScalar<T>::deserialize(const Json::Value& token) { if (isDeserilizationPossible(token)){ if (token.isInt()) { myValue = token.asInt(

问题方法:

template <typename T>
    void SerializableScalar<T>::deserialize(const Json::Value& token)
    {
        if (isDeserilizationPossible(token)){

            if (token.isInt())
            {
                myValue = token.asInt();
            }
            if (token.isDouble())
            {
                myValue = token.asDouble();
            }
            if (token.isString())
            {
                myValue = token.asString().c_str();
            }
            if (token.isBool())
            {
                myValue = token.asBool();
            }
        }
    }
创建对象时:

SerializableScalar<int> object;
object.deserialize(token);

不幸的是,没有办法为每个相关类型创建专门化。以下是将专门化委托给帮助器类的一种方法:

template<typename T>
struct Helper
{
    static inline bool is(const Json::Value&);
    static inline T as(const Json::Value&);
};

template<> bool Helper<int>::is(const Json::Value& tok)
{ return tok.isInt(); }

template<> bool Helper<double>::is(const Json::Value& tok)
{ return tok.isDouble(); }

template<> bool Helper<string>::is(const Json::Value& tok)
{ return tok.isString(); }

template<> bool Helper<bool>::is(const Json::Value& tok)
{ return tok.isBool(); }

template<> int Helper<int>::as(const Json::Value& tok)
{ return tok.asInt(); }

template<> double Helper<double>::as(const Json::Value& tok)
{ return tok.asDouble(); }

template<> string Helper<string>::as(const Json::Value& tok)
{ return tok.asString(); }

template<> bool Helper<bool>::as(const Json::Value& tok)
{ return tok.asBool(); }

template<typename T>
void SerializableScalar<T>::deserialize(Json::Value& tok)
{
    if ( Helper<T>::is(tok) )
        myValue = Helper<T>::as(tok);
}
模板
结构辅助程序
{
静态内联bool是(constjson::Value&);
静态内联T-as(constjson::Value&);
};
模板bool-Helper::is(const-Json::Value&tok)
{return tok.isInt();}
模板bool-Helper::is(const-Json::Value&tok)
{return tok.isDouble();}
模板bool-Helper::is(const-Json::Value&tok)
{return tok.isString();}
模板bool-Helper::is(const-Json::Value&tok)
{return tok.isBool();}
模板int-Helper::as(const-Json::Value&tok)
{return tok.asInt();}
模板双助手::as(constjson::Value&tok)
{return tok.asDouble();}
模板字符串助手::as(constjson::Value&tok)
{return tok.asString();}
模板bool-Helper::as(const-Json::Value&tok)
{return tok.asBool();}
模板
void SerializableScalar::反序列化(Json::Value&tok)
{
if(Helper::is(tok))
myValue=Helper::as(tok);
}

不幸的是,没有办法为每个相关类型创建专门化。以下是将专门化委托给帮助器类的一种方法:

template<typename T>
struct Helper
{
    static inline bool is(const Json::Value&);
    static inline T as(const Json::Value&);
};

template<> bool Helper<int>::is(const Json::Value& tok)
{ return tok.isInt(); }

template<> bool Helper<double>::is(const Json::Value& tok)
{ return tok.isDouble(); }

template<> bool Helper<string>::is(const Json::Value& tok)
{ return tok.isString(); }

template<> bool Helper<bool>::is(const Json::Value& tok)
{ return tok.isBool(); }

template<> int Helper<int>::as(const Json::Value& tok)
{ return tok.asInt(); }

template<> double Helper<double>::as(const Json::Value& tok)
{ return tok.asDouble(); }

template<> string Helper<string>::as(const Json::Value& tok)
{ return tok.asString(); }

template<> bool Helper<bool>::as(const Json::Value& tok)
{ return tok.asBool(); }

template<typename T>
void SerializableScalar<T>::deserialize(Json::Value& tok)
{
    if ( Helper<T>::is(tok) )
        myValue = Helper<T>::as(tok);
}
模板
结构辅助程序
{
静态内联bool是(constjson::Value&);
静态内联T-as(constjson::Value&);
};
模板bool-Helper::is(const-Json::Value&tok)
{return tok.isInt();}
模板bool-Helper::is(const-Json::Value&tok)
{return tok.isDouble();}
模板bool-Helper::is(const-Json::Value&tok)
{return tok.isString();}
模板bool-Helper::is(const-Json::Value&tok)
{return tok.isBool();}
模板int-Helper::as(const-Json::Value&tok)
{return tok.asInt();}
模板双助手::as(constjson::Value&tok)
{return tok.asDouble();}
模板字符串助手::as(constjson::Value&tok)
{return tok.asString();}
模板bool-Helper::as(const-Json::Value&tok)
{return tok.asBool();}
模板
void SerializableScalar::反序列化(Json::Value&tok)
{
if(Helper::is(tok))
myValue=Helper::as(tok);
}

这应该是一个注释,但它太长了:

这种方法的问题 编译时和运行时数量不能很好地结合在一起。我想您已经发现将类型
int
硬编码到序列化中会让您感到不舒服

SerializableScalar<int> object;
  • 其次,更复杂的是,您可以返回一个
    可选的
    ,如果json没有填充,它将处于null状态

  • 一般情况
    但是,在一般情况下,当您不知道内容或内容即将更改时,必须接受Json的多类型和相应的模型。基本方法是使用一个
    boost::variant
    加上一个适当的。

    这应该是一个注释,但它太长了:

    这种方法的问题 编译时和运行时数量不能很好地结合在一起。我想您已经发现将类型
    int
    硬编码到序列化中会让您感到不舒服

    SerializableScalar<int> object;
    
  • 其次,更复杂的是,您可以返回一个
    可选的
    ,如果json没有填充,它将处于null状态

  • 一般情况
    但是,在一般情况下,当您不知道内容或内容即将更改时,必须接受Json的多类型和相应的模型。基本方法是使用
    boost::variant
    加上适当的。

    一种可能是创建一个全面重载:

    void Json::Value::operator=(...)
    {
        assert( false && "this should never be called" );
    }
    

    这将在超分辨率中具有最低优先级,然而,Erric赋值将不再产生编译时错误,因此认为它是一个快速修复。 一种可能是创建一个全面过载:

    void Json::Value::operator=(...)
    {
        assert( false && "this should never be called" );
    }
    

    这将在超分辨率中具有最低优先级,然而,Erric赋值将不再产生编译时错误,因此认为它是一个快速修复。 如果包含编译器错误,并且可能包含SerializableScallar的进一步类定义,这将非常有用。什么是

    myValue
    ?更新了,包括错误在内,真的没有办法解决。您必须为您关心的每个
    T
    编写一个模板专门化。但是,您可以专门处理类型的“组”。请参阅这里的答案以供参考:基本上,没有办法确保在编译时在运行时遍历if语句的任何分支。这就是编译器阻塞的原因。例如,对于
    int
    s,它无法提前知道
    token.isInt()
    是否只会返回
    true
    。如果包含编译器错误,并可能包含SerializableScalar的进一步类定义,这将非常有用。什么是
    myValue
    ?更新了,包括错误在内,真的没有办法解决。您必须为您关心的每个
    T
    编写一个模板专门化。但是,您可以专门处理类型的“组”。请参阅这里的答案以供参考:基本上,没有办法确保在编译时在运行时遍历if语句的任何分支。这就是编译器阻塞的原因。例如,它无法提前知道
    token.isInt()
    是否只为
    int
    s返回
    true
    。实际上,这看起来没那么糟糕,我喜欢这个解决方案。谢谢,事实上这看起来没那么糟糕,我喜欢这个解决方案。谢谢谢谢,看起来不错,我知道json的上下文,json只是一个值谢谢,看起来不错,我知道json的上下文,json只是一个值
    template<typename T>
    auto get_value(Json::Value const& json) {}
    
    template<>
    auto get_value<int>(Json::Value const& json) { return json.as_int(); }
    
    template<>
    auto get_value<bool>(Json::Value const& json) { return json.as_bool(); }
    
    constexpr int DEFAULT = -1;
    template<>
    auto get_value<int>(Json::Value const& json)
    {
        return json.is_int() ? json.as_int() : DEFAULT;
    }
    
    void Json::Value::operator=(...)
    {
        assert( false && "this should never be called" );
    }