模板函数使用类型推导时的bug

template
static bool parse_a_value(T& val, Json::Value json_val)
{
if(json_val.isNull()) return false;

if(typeid(val) == typeid(int)
    || typeid(val) == typeid(int16_t)
    || typeid(val) == typeid(int8_t)
    || typeid(val) == typeid(int32_t))
{
    val = json_val.asInt();
}

if(typeid(val) == typeid(uint8_t)
    || typeid(val) == typeid(uint16_t)
    || typeid(val) == typeid(uint32_t))
{
    val = json_val.asUInt();
}

if(typeid(val) == typeid(int64_t)) val = T(json_val.asInt64());
if(typeid(val) == typeid(uint64_t)) val = T(json_val.asUInt64());
if(typeid(val) == typeid(float)) val = T(json_val.asFloat());
if(typeid(val) == typeid(double)) val = T(json_val.asDouble());
// if(typeid(val) == typeid(std::string)) val = T(json_val.asString());

return true;

}

25行报错

./task_control: /usr/local/lib/libuuid.so.1: no version information available (required by ./task_control)
[2023-12-21 15:51:28.811] [console] [info] [main.cpp:138] =====>val type:i/6253375586064260614 NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE/5774750460303204477, false
^C
huanglidi@hld-vm:~/work/code/balcony/balcony_painting/spray_control/build$ 
huanglidi@hld-vm:~/work/code/balcony/balcony_painting/spray_control/build$ make -j8
Scanning dependencies of target task_control
[  1%] Building CXX object CMakeFiles/task_control.dir/src/route_generate.cpp.o
/home/huanglidi/work/code/balcony/balcony_painting/spray_control/src/route_generate.cpp: In instantiation of ‘bool parse_a_value(T&, Json::Value) [with T = double]’:
/home/huanglidi/work/code/balcony/balcony_painting/spray_control/src/route_generate.cpp:83:49:   required from here
/home/huanglidi/work/code/balcony/balcony_painting/spray_control/src/route_generate.cpp:70:48: error: invalid cast from type ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to type ‘double’
     if(typeid(val) == typeid(std::string)) val = T(json_val.asString());
                                                ^
/home/huanglidi/work/code/balcony/balcony_painting/spray_control/src/route_generate.cpp: In instantiation of ‘bool parse_a_value(T&, Json::Value) [with T = unsigned int]’:
/home/huanglidi/work/code/balcony/balcony_painting/spray_control/src/route_generate.cpp:87:47:   required from here
/home/huanglidi/work/code/balcony/balcony_painting/spray_control/src/route_generate.cpp:70:48: error: invalid cast from type ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to type ‘unsigned int’
CMakeFiles/task_control.dir/build.make:964: recipe for target 'CMakeFiles/task_control.dir/src/route_generate.cpp.o' failed

测试代码

#include <typeinfo>

template<typename T>
static bool parse_a_value(T& val,Json::Value &root)
{
    bool b = (typeid(val) == typeid(std::string));

    Rotating_Info("=====>val type:{}/{} {}/{}, {}", typeid(val).name(), 
    typeid(val).hash_code(),
    typeid(std::string).name(), typeid(std::string).hash_code(), b);
    
    if(b) 
    {
        Rotating_Info("=====>1111");
        // val = root.asString();
    }
    return true;
}


## 原因与解决方法?
posted @ 2024-04-12 16:02  HL棣  阅读(6)  评论(0编辑  收藏  举报