Passing a parameter package to replace the stl function causes a compilation error

As defined by emplace_back, void emplace_back (Args&&... args); is a function of the variation pattern. So, I wrote the following:

 #include <vector> int main() { std::vector<int> myvector2(10,0); myvector2.emplace_back(1,2,3,4,5,6); } 

The compiler complains:

 g++ -std=c++0x stlstudy.cc ' Internal compiler error: Error reporting routines re-entered. Please submit a full bug report, with preprocessed source if appropriate. See <file:///usr/share/doc/gcc-4.7/README.Bugs> for instructions. Preprocessed source stored into /tmp/cc7q32tE.out file, please attach this to your bugreport. 

And OS alerts:

 Sorry, Ubuntu 13.04 has experienced an internal error. 

The file /tmp/cc7q32tE.out too long to be published here, and this may not help. Am I doing something wrong or a compilation error? I do not understand.

After comments and bug report: jrok gives a very good explanation of why this is happening. I used gcc 4.7, I reported an error, and received the following response:

 Jonathan W***** <redi at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Known to work| |4.8.0 --- Comment #1 from Jonathan W***** <redi at gcc dot gnu.org> --- Seems to be fixed for 4.8 already. 
+4
source share
1 answer

An internal compiler error is not your error. Compilers should give meaningful diagnostics in case of poorly formed input, and not just a failure on you.

However, the number and types of emplace_back arguments must match one of the constructors of the vector value type. You have an int s vector, so you can pass no more than one argument, which either has the appropriate type or is implicitly converted to value_type . (You can leave the argument list empty - to build the object using the default constructor).

 std::vector<int> v; v.emplace_back(1); // ok v.emplace_back(1.0); // ok v.emplace_back(1, 2); // not ok, there no constructor for `int` that takes two ints 

The purpose of emplace_back is not to insert multiple elements into the same statement (I got the impression that this is what you expected from this, I thought it was recently), but to create the element in place, forwarding arguments to the constructor and excluding copies).

Gcc 4.8. performs the error , although the error message is not auxiliary.

+7
source

Source: https://habr.com/ru/post/1482400/


All Articles