正在复制分配增压选项_说明
Copying assigning boost options_descriptions
我试图在类中存储boost::program_options::options_description
,但我无法为我的类编写赋值运算符,因为options_description有一个const成员。或者至少我是这样理解这个问题的。
下面是一个我的类不会编译的例子:
struct command
{
command()
{
}
command(const std::string& name,
const po::options_description& desc)
: name(name), desc(desc)
{
}
command& operator=(const command& other)
{
name = other.name;
desc = other.desc; // problem here
return *this;
}
~command()
{
}
std::string name;
po::options_description desc;
};
/usr/include/boost/program_options/options_description.hpp:173:38:
error: non-static const member
‘const unsigned int boost::program_options::options_description::m_line_length’,
can’t use default assignment operator
/usr/include/boost/program_options/options_description.hpp:173:38:
error: non-static const member
‘const unsigned int boost::program_options::options_description::m_min_description_length’,
can’t use default assignment operator
最初这是一个自我回答的问题。然后我意识到:
command& operator=(const command& other)
{
name = other.name;
desc.add(other.desc);
return *this;
}
将把other.desc附加到desc,这不是我想要的。
因此,这只是意味着options_description
不可复制。要做到这一点,请使其成为shared_ptr
(具有共享所有权语义[1])或具有适当clone
操作[2]的value_ptr
。
基于shared_ptr
的简单演示:在Coliru上直播
#include <boost/program_options.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
namespace po = boost::program_options;
struct command {
command(const std::string& name = {},
const po::options_description& desc = {})
: name(name),
desc(boost::make_shared<po::options_description>(desc))
{
}
command& operator=(const command& other) = default;
private:
std::string name;
boost::shared_ptr<po::options_description> desc;
};
int main() {
command a, b;
b = a;
}
[1]options_description
已经在内部使用了这些,所以不会突然产生大的开销
[2]参见例如。http://www.mr-edd.co.uk/code/value_ptr对于在网络间浮动的许多中的一个
相关文章:
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- Win32编译器选项和内存分配
- C/C++预处理器是否可以检测一些编译器选项
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 当使用通配符和null指针调用函数时,对输出的说明
- 通过选项卡的文本设置QTabWidget顺序
- 通过ccmake在cmake中缓存依赖选项
- 关于C++中具有多重继承"this"指针的说明
- 如何传递多个 std::文件系统选项?
- 基于编译器选项的编译二进制路径
- "perf_event_attr"结构的"read_format"属性的选项到底是什么?
- 如何应用 libcurl 的持久连接选项
- 16 位到 10 位转换代码说明
- 文件中.dat Dlib 选项
- LLVM | codegen 用于带有命令行选项的程序输入功能
- 编译 Boost 时在 OS X 上的"ld:未知选项:-soname"
- 按钮悬停在 QT 中垂直布局的选项卡小部件中不起作用
- C++ 基本 CTOR 说明 - 为什么不调用赋值/复制构造函数
- 正在复制分配增压选项_说明