我有一些东西可以在 gcc 4.9 中编译,但在 gcc 4.7 中失败。
它是一个具有移动构造函数的类,但我将其复制构造函数设置为私有(private):
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 声称有一个两屏长的错误,声称其复制构造函数是私有(private)的:
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
由于我的一些用户使用的系统非常旧,可能使用的是旧版本的编译器,我真的很想知道是否有任何解决方法。
最佳答案
如果您从不使用复制构造函数,而不是将其设为私有(private),您可以将其删除:
Option(const Option&) = delete;
这可能有助于编译器选择正确的、可用的构造函数。
否则您可以尝试手动构建一个临时对象并将其移回:
options.push_back(Option("help", 'h', OPTION_GROUP_MISC,
&opt_show_help, htio2::Option::FLAG_NONE,
"Show help and exit."));
关于c++ - 如何解决 gcc 4.7 和 4.9 之间 std::vector 的不同行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27764068/