我已经阅读了boost :: property_tree的文档,但没有找到更新或合并ptree与另一个ptree的方法.我该怎么做呢?
鉴于下面的代码,update_ptree函数将如何?
#include <iostream>
#include <boost/property_tree/ptree.hpp>
using boost::property_tree::ptree;
class A
{
ptree pt_;
public:
void set_ptree(const ptree &pt)
{
pt_ = pt;
};
void update_ptree(const ptree &pt)
{
//How do I merge/update a ptree?
};
ptree get_ptree()
{
return pt_;
};
};
int main()
{
A a;
ptree pt;
pt.put<int>("first.number",0);
pt.put<int>("second.number",1);
pt.put<int>("third.number",2);
a.set_ptree(pt);
ptree pta = a.get_ptree();
//prints "0 1 2"
std::cout << pta.get<int>("first.number") << " "
<< pta.get<int>("second.number") << " "
<< pta.get<int>("third.number") << "\n";
ptree updates; …Run Code Online (Sandbox Code Playgroud) 为什么允许在gcc中编译模板版本?它是编译器错误还是与模板一起使用时实际上有效?有人可以向我解释一下吗?
它不能在godbolt.org上使用的clang或其他编译器上编译.
编译错误由constexpr中使用的字符串和字符串流生成.
#include <iostream>
#include <string>
#include <sstream>
template<typename T>
constexpr std::string func1(T a, T b) //Compiles and runs
{
std::stringstream ss;
ss << a << b << a+b;
return ss.str();
}
constexpr std::string func2(int a, int b) //Compile error
{
std::stringstream ss;
ss << a << b << a+b;
return ss.str();
}
int main()
{
int a = 5;
int b = 7;
std::cout << func1(a,b) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 背景:
我想将一些静态库链接到共享库中。原因是我希望我的应用程序使用我已经测试过的特定库版本。我不想将静态版本作为共享库与我的应用程序一起提供。我创建了这个示例库和应用程序以尽可能简化。我想在链接期间继续将共享库链接到应用程序。
问题:
为什么我会收到下面列出的错误消息?我究竟做错了什么?也许这不是 Linux 上通常的做法,但是可以这样做吗?这个提升有针对性吗?
- - 图书馆
//example.cpp
#include <boost/thread.hpp>
void doit()
{
boost::thread t1;
}
#build script
g++ -Wall -fPIC -I/usr/include -c example.cpp -o example.o
g++ -shared /usr/lib/libboost_thread.a /usr/lib/libboost_system.a
example.o -o libexample.so
#build OK.
Run Code Online (Sandbox Code Playgroud)
---- 申请样本
//main.cpp
#include <iostream>
void doit();
int main()
{
std::cout << "main\n";
doit();
return 0;
};
#build script.
g++ -Wall -c main.cpp -o main.o
g++ libexample.so main.o -o main
#error message.
libexample.so: undefined reference to `boost::thread::thread()'
libexample.so: undefined reference to `boost::thread::~thread()'
collect2: …Run Code Online (Sandbox Code Playgroud) 一些背景:
前几天我遇到了一些让我想到嵌套函数调用中的重载解析的东西.请考虑以下代码:
#include <iostream>
void printer(const int &a)
{
std::cout << a << "\n";
}
const int& func(const int &a)
{
std::cout << "const int& ";
return a;
}
int& func(int &a)
{
std::cout << "int& ";
return a;
}
int main()
{
int a = 42;
const int b = 21;
printer(func(a));
printer(func(b));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码打印
int& 42
const int& 21
Run Code Online (Sandbox Code Playgroud)
所以,显然func(a)看到a是非const int.编译器还必须看到打印机函数需要const int&argument.并且存在一个返回const int&的func(...).我查看了C++标准,它说const refs和refs被认为是不同的参数类型(这就是为什么它选择int&for func(a)).
对于这个问题:
在调用func(a)时,编译器是否允许使用func(const int&)版本而不是func(int&)?
(如果它看到结果传递给想要const int¶meter的函数,则可能存在某种优化可能性.)
是否有可能使unique_ptr适应普通c?
也许如果有一种方法可以在调用malloc/free时模拟对自制"构造函数/析构函数"的调用?
它可行吗?或者这只是一个愚蠢的想法?
c++ ×5
boost ×1
c ×1
c++14 ×1
const ×1
constexpr ×1
g++ ×1
gcc ×1
linux ×1
optimization ×1
overloading ×1
shared ×1
static ×1
unique-ptr ×1