1

我正在使用 nlohmann 的 JSON 库来解析 JSON 文件。在这种情况下,我想知道在传递对象相关的效率和开销方面,按值或引用传递 JSON 对象是否是最佳实践。

这是一个简短的示例,显示我将按值传递它:

#include <iostream>
#include <string>
#include <fstream>
#include "json.hpp"

void read_json(nlohmann::json j) /// pass by value or reference?
{
    ///...
}

int main()
{
    std::string file_name = "asd.json";

    std::ifstream filename_stream(file_name);
    nlohmann::json j = nlohmann::json::parse(filename_stream);

    read_json(j);

    return 0;
}   
4

0 回答 0