小编Sth*_*ing的帖子

在C++中,使用模板函数调用指针值的模板函数

我试图为我的调用函数中作为模板参数给出的指针调用模板函数.我的代码是:

template <>
struct serialize_helper<std::string> {
     // not important code...
    }

};

template <class T>
inline void serializer(const T& obj, StreamType::iterator& res) {

    if(std::is_pointer<T>::value)
    {
        //THIS doesn' work
        serialize_helper<*T>::apply(*obj,res);
    }
    else
        serialize_helper<T>::apply(obj,res);

}
Run Code Online (Sandbox Code Playgroud)

如果我打电话:

std::string test("test");
serializer(test, res);
Run Code Online (Sandbox Code Playgroud)

一切正常.但我也希望能够使用指针调用序列化程序作为obj:

std::string test* = new std::string("test");
serializer(test, res);
Run Code Online (Sandbox Code Playgroud)

在调用序列化函数之前取消引用指针不是一个可能的选择,请不要这样做.在串行器功能内部是可能的.

更简短的描述:我想serializer用a 来调用std::string*并且如果我用std::string它指向的话调用它就会做同样的事情.

c++ templates pointers c++11

1
推荐指数
1
解决办法
90
查看次数

标签 统计

c++ ×1

c++11 ×1

pointers ×1

templates ×1