The following example program doesn't compile for me on either clang 3.1 or gcc 4.8:
#include <fstream>
#include <vector>
using namespace std;
int main()
{
vector<ifstream> bla;
bla.emplace_back("filename");
return 0;
}
However, I thought emplace_back should
"Insert a new element at the end of the vector, right after its current last element. This new element is constructed in place using args as the arguments for its construction."
Does anyone know why this doesn't compile then? did I misunderstand or are the library implementations not yet complete?
The vector::emplace() is an STL in C++ which extends the container by inserting a new element at the position. Reallocation happens only if there is a need for more space. Here the container size increases by one.
emplace_back(): This method is used instead of creating the object using parameterized constructor and allocating it into a different memory, then passing it to the copy constructor, which will insert it into the vector. This function can directly insert the object without calling the copy constructor.
Streams in c++11 are movable, so in theory you should be able to do what you want, problem is that movable streams have not yet been implemented in gcc/libstdc++.
To back up my answer further please take a look at gcc/libstdc++ c++11 status: http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2011
Especially 27.5, 27.8, 27.9
That's a bug in the implementation of std::basic_istream
. It should be movable, but isn't; and only movable types can be stored in a vector.
Here is the bug report against GCC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With