昨天,我在团队的代码中使用 jsoncpp 库遇到了这样的函数:
#include <json/value.h>
Json::Value makeList() {
Json::Value list(Json::arrayValue);
list.append(new Json::Value(true));
return list;
}
Run Code Online (Sandbox Code Playgroud)
它引起了我的注意,因为new在调用中使用list.append泄漏了一些内存,这可以通过简单地删除来修复new。然而,在调试过程中,我意识到Json::Value::append需要aconst Json::Value&或aJson::Value&&。C++ 不会将指针隐式转换为引用,因此 aJson::Value*不应隐式转换为其中任何一个。
为了证实这一点,我写了自己的简短示例:
#include <iostream>
class Foo {
public:
int x = 4;
};
void printValue(const Foo& f) { std::cout << "The value is " << f.x << ".\n"; }
void printValue(Foo&& f) { std::cout << "The value is " << f.x << ".\n"; }
int main() {
printValue(new Foo());
}
Run Code Online (Sandbox Code Playgroud)
事实上,这会导致编译错误:
own-example.cpp:12:5: error: no matching function for call to 'printValue'
printValue(new Foo());
^~~~~~~~~~
own-example.cpp:8:6: note: candidate function not viable: no known conversion from 'Foo *' to 'const Foo' for 1st argument; dereference the argument with *
void printValue(const Foo& f) { std::cout << "The value is " << f.x << ".\n"; }
^
own-example.cpp:9:6: note: candidate function not viable: no known conversion from 'Foo *' to 'Foo' for 1st argument; dereference the argument with *
void printValue(Foo&& f) { std::cout << "The value is " << f.x << ".\n"; }
^
Run Code Online (Sandbox Code Playgroud)
那么,为什么我发现的代码调用了Json::Value::append编译,而玩具示例却没有?
Json::Value(new Json::Value())编译是因为
bool x = new Json::Value();由于 C++ 规则允许从指针隐式转换bool为, 和Json::Value有一个构造函数重载,它需要一个bool.| 归档时间: |
|
| 查看次数: |
51 次 |
| 最近记录: |