链式重载下标运算符 [] 的编译失败
Compile fails for chained overloaded subscript operator[]
我正在尝试围绕一些解析库(JSON、YAML 等)创建一个精简的包装器,这将允许我使用统一的语法,而不管我使用的 file-type/parser。我希望包装器利用模板,这样我就不必在 运行 时进行任何动态检查来检查我正在使用哪个库(这在一定程度上是学术追求)。
包装器结构的重要部分在这里:
template<typename K> struct Wrapper
{
K node; // Element that is wrapped
Wrapper() {};
Wrapper(K impl) : node(impl) {};
Wrapper(const Wrapper<K>& other) : node(other.node) {};
const Wrapper<K> operator[](const char* key);
//... Other stuff
}
我的问题是当我尝试将多个 []
操作链接在一起时遇到编译时错误。
可以在此处找到 operator[]
重载:
// Returning by value since I am creating an object that goes out of scope.
// This is okay because the parsing is read only.
template<> const Wrapper<to_wrap> Wrapper<to_wrap>::operator[](const char* key)
{
// It is safe to assume that node[key] produces a to_wrap type.
return Wrapper<to_wrap>(node[key]);
}
关于如何称呼它的一些例子:
template<typename T> bool configure(T config)
{
Wrapper<T> root(config);
// Method A
Wrapper<T> thing = root["field1"]["field2"];
// Method B
Wrapper<T> first_thing = root["field1"];
Wrapper<T> second_thing = first_thing["field2"];
}
如果我尝试 Method A
,则会发生编译时错误。 Method B
会在编译和 运行 时产生我期望的结果:一个包含适当 node
的 Wrapper
对象。来自 A
的错误如下:
error: conversion from ‘const char’ to non-scalar type ‘Wrapper<to_wrap>’ requested Wrapper<T> thing = root["field1"]["field2"];
这让我认为编译器推断类型的方式存在问题,但我不完全确定。任何 help/insights 将不胜感激!
改变
const Wrapper<K> operator[](const char* key);
到
const Wrapper<K> operator[](const char* key) const;
或者 - 更好的是 - 制作 operator[]
.
的 const 和非常量版本
Wrapper<K> operator[](const char* key);
const Wrapper<K> operator[](const char* key) const;
您当前的operator[]
returns一个const
对象,和不允许在const
对象上使用。
我正在尝试围绕一些解析库(JSON、YAML 等)创建一个精简的包装器,这将允许我使用统一的语法,而不管我使用的 file-type/parser。我希望包装器利用模板,这样我就不必在 运行 时进行任何动态检查来检查我正在使用哪个库(这在一定程度上是学术追求)。
包装器结构的重要部分在这里:
template<typename K> struct Wrapper
{
K node; // Element that is wrapped
Wrapper() {};
Wrapper(K impl) : node(impl) {};
Wrapper(const Wrapper<K>& other) : node(other.node) {};
const Wrapper<K> operator[](const char* key);
//... Other stuff
}
我的问题是当我尝试将多个 []
操作链接在一起时遇到编译时错误。
可以在此处找到 operator[]
重载:
// Returning by value since I am creating an object that goes out of scope.
// This is okay because the parsing is read only.
template<> const Wrapper<to_wrap> Wrapper<to_wrap>::operator[](const char* key)
{
// It is safe to assume that node[key] produces a to_wrap type.
return Wrapper<to_wrap>(node[key]);
}
关于如何称呼它的一些例子:
template<typename T> bool configure(T config)
{
Wrapper<T> root(config);
// Method A
Wrapper<T> thing = root["field1"]["field2"];
// Method B
Wrapper<T> first_thing = root["field1"];
Wrapper<T> second_thing = first_thing["field2"];
}
如果我尝试 Method A
,则会发生编译时错误。 Method B
会在编译和 运行 时产生我期望的结果:一个包含适当 node
的 Wrapper
对象。来自 A
的错误如下:
error: conversion from ‘const char’ to non-scalar type ‘Wrapper<to_wrap>’ requested Wrapper<T> thing = root["field1"]["field2"];
这让我认为编译器推断类型的方式存在问题,但我不完全确定。任何 help/insights 将不胜感激!
改变
const Wrapper<K> operator[](const char* key);
到
const Wrapper<K> operator[](const char* key) const;
或者 - 更好的是 - 制作 operator[]
.
Wrapper<K> operator[](const char* key);
const Wrapper<K> operator[](const char* key) const;
您当前的operator[]
returns一个const
对象,和不允许在const
对象上使用。