部分C++ STL 容器的模板专用化
Partial C++ Template Specialization with STL containers
所以我的目标是创建一个可以返回不同值的函数或函子。用例是将 Json 对象转换为正确的C++类型。在大多数情况下,我都有这个工作,但我遇到了 std::vector
和 std::array
等容器的问题。我知道你不能重载一个函数来只返回不同的值类型,所以我一直在使用具有模板专用化的struct
。我想我需要使用部分模板专用化来实现这一点,但我遇到了编译错误。这是我的代码的样子:
template <typename T>
struct from_json
{
T operator()(const Json::Value &json)
{
return T::fromJson(json);
}
};
template <>
struct from_json<std::string>
{
std::string operator()(const Json::Value &json)
{
return std::string(json.asCString());
}
};
template <typename T>
struct from_json<std::vector<T> >
{
std::vector<T> operator()(const Json::Value &json)
{
std::vector<T> vector;
for (auto obj : json)
vector.push_back(from_json(obj));
return vector;
}
};
关于代码片段的一些说明:我有一个抽象类,它要求fromJson
由我自己的可序列化类实现,这就是一般情况。std::string
概括似乎工作正常。std::vector
是事情破裂的地方。编译器认识到这是应该使用的专用化,但在尝试实际使用它时会给出错误。
std::vector<std::string> my_vector = from_json<std::vector<std::string> >(json["my_vector"]);
编译器错误如下:
error: no matching function for call to 'from_json<std::vector<std::__cxx11::basic_string<char> > >::from_json(const Json::Value&)'
std::vector<std::string> my_vector = from_json<std::vector<std::string> >(json["my_vector"]);
note: candidate: constexpr from_json<std::vector<std::__cxx11::basic_string<char> > >::from_json()
还值得注意的是,我使用的是C++ 11。
如果有更好的方法,我绝对愿意接受这方面的建议,可以做得更好。
谢谢!
所以在这一行中:
vector.push_back(from_json(obj));
代码中的from_json
是具有已定义operator()
的类模板。它不是一个函数或一个函数对象(从这个意义上说,它有点像std::hash
)。所以你不能调用它 - 你需要调用它的实例化。
我猜你的意思是这样的:
vector.push_back(from_json<T>{}(obj));
或者,如果你属于范围 v3:
std::vector<T> operator()(const Json::Value &json)
{
return json | view::transform(from_json<T>{});
}
相关文章:
- .cpp和.h文件中的模板专用化声明
- 调用专用模板时出错"no matching function for call to [...]"
- 模板专用化(按容器):value_type
- 静态数据成员模板专用化的实例化点在哪里
- 特征 3 类的模板专用化
- Visual Studio 2017 不允许我创建 C++ 专用模板
- 字符串化递归的"std::vector<std::vector<...>>"而不使用部分模板函数专用化
- 具有常量引用参数的函数模板专用化
- 使用其他模板的模板专用化
- 使用专用显卡进行 OpenGL 渲染时帧速率较低
- 嵌套模板显式专用化
- 如何检查模板专用化是否是基本模板的子类?
- C++:部分模板专用化用例
- 通过依赖类型使用非类型模板参数的单类型模板参数类模板的部分专用化
- 使用对象的基类部分模板专用化对对象进行哈希处理::哈希
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- 为什么依赖模板类型在部分专用化中不可推导?
- 如何为静态常量模板化专用整数值分配存储
- 模板类的部分模板专用化,如 std::function
- 合并一组模板专用化