如何解决 gcc 4.7 和 4.9 之间 std::vector 的不同行为
How to work around the different behaviour of std::vector between gcc 4.7 and 4.9?
我有一些东西可以在 gcc 4.9 中编译,但在 gcc 4.7 中失败了。
它是一个具有移动构造函数的类,但我将其复制构造函数设置为私有:
class Option
{
public:
Option(const std::string& name_long,
char name_short,
const std::string& group,
bool* value_store,
int32_t flags,
const std::string& option_desc);
// and several similar constructors
Option(Option&& other);
private:
Option(const Option& other);
};
当 vector 的 emplace_back()
函数调用它时,会出现此问题:
// options is a std::vector<Option>
options.emplace_back("help", 'h', OPTION_GROUP_MISC,
&opt_show_help, htio2::Option::FLAG_NONE,
"Show help and exit.");
这在 gcc 4.9 中成功编译并运行良好,但 gcc 4.7 声称存在两个屏幕长的错误,声称其复制构造函数是私有的:
In file included from /public/yx/works/writing_tcrklass2/src/TCRklass2-1.90.0-Source/src/tcrk2/App.h:4:0,
from /public/yx/works/writing_tcrklass2/src/TCRklass2-1.90.0-Source/src/tcrk2/App.cpp:1:
......
/public/yx/works/writing_tcrklass2/src/TCRklass2-1.90.0-Source/src/tcrk2/App.cpp:58:47: required from here
/usr/local/include/htio2/OptionParser.h:188:5: error: ‘htio2::Option::Option(const htio2::Option&)’ is private
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/move.h:57:0,
......
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/type_traits:762:43: error: within this context
由于我的一些用户有非常旧的系统,它可能使用旧版本的编译器,我真的很想知道是否有任何解决方法。
如果您从不使用复制 ctor,而不是将其设为私有,您可以将其删除:
Option(const Option&) = delete;
这可能有助于编译器选择正确的可用 CTOR。
否则,您可以尝试手动构建临时对象并将其移回:
options.push_back(Option("help", 'h', OPTION_GROUP_MISC,
&opt_show_help, htio2::Option::FLAG_NONE,
"Show help and exit."));
g++ 4.7 想要一个 noexcept move 构造函数:
Option(Option&& other) noexcept {};
如果你不想复制,你可以使用shared_ptr。
class Option;
typedef boost::shared_ptr<Option> OptionPtr;
OptionPtr o = boost::make_shared<Option>("help", 'h', OPTION_GROUP_MISC,
&opt_show_help, htio2::Option::FLAG_NONE,
"Show help and exit.");
std::vector<OptionsPtr> options;
options.push_back(o);
相关文章:
- 尝试使用 std::vector<std::thread时出现静态断言失败错误>
- 字符串化递归的"std::vector<std::vector<...>>"而不使用部分模板函数专用化
- 连接和压缩标准::vector<std::字符串的最佳方法>
- C++从 std::vector<std::function<中删除 std::function>>
- 如何在构造函数初始值设定项列表中使用 n 个元素初始化 std::vector<std::time_t>
- 如何使用 CUDA 将 std::vector<std::string> 复制到 GPU 设备
- 编译错误 std::vector<std::shared_ptr<T>>迭代器和擦除方法
- 将 std::vector<std::unique_ptr<T>> 移动到 std::vector<std::shared_ptr<T>>
- 如何从 boost::container::vector<std::string>::iterator 访问索引和对象?
- 使用 std::vector<std::future<int>> 和 std::async 启动几个线程时中止
- 为什么转置这个 std::vector<std::vector<std::string> > 这么慢?
- 是否有可能在没有复制的情况下传递 std::vector<int> 作为参数来获得 std::vector<std::array<int, 3>>?
- 在 boost::<double>asio::buffer 中使用像 std::vector<std::complex> 这样的参数
- 如何从 std::initializer_list<char const* 构建 std::vector<std::string>>
- 如何检查 std::vector<std::string> 的元素是否以某个子字符串开头?
- 在 std::vector<std::vector 中重新存储内部向量<TYPE>>
- 将 std::vector<std::string> 转换为 const char* const*
- 不能将结构push_back() 转换为 std::vector<std::shared_ptr<theStruct>> theVector
- SWIG:传递一个 std::vector< std::vector <double> >指向 python 的指针
- 将 std::vector<std::p air<const K, V>*> 转换为 std::vector<std::p air<const K, V>&g