How to deal with different std :: vector behavior between gcc 4.7 and 4.9?
I have something that can be compiled in gcc 4.9, but gcc 4.7 failed.
This is a class that has a move constructor, but I set its copy constructor to 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);
};
The problem arises when it is called by a vector function 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.");
This compiles successfully and works well with gcc 4.9, but gcc 4.7 claims to have a two screen issue, claiming its copy constructor is 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
As some of my users have a very old system that is probably using an older version of the compilers, I really want to know if there is a way around it.
source to share
If you never use a copy of the ctor, instead of making it private, you can delete it:
Option(const Option&) = delete;
this can help the compiler find the correct ctor available.
Otherwise, you can try to create a temporary object manually and move it back:
options.push_back(Option("help", 'h', OPTION_GROUP_MISC,
&opt_show_help, htio2::Option::FLAG_NONE,
"Show help and exit."));
source to share
If you don't want to copy, you can use 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);
source to share