Thread Vector in C ++ 11

Following code

vector<ofstream> v;
v.emplace_back("file1.txt");
v.emplace_back("file2.txt");
for (int i = 0, ilen = v.size(); i < ilen; ++i)
    v[i] << "Test" << i << endl;

compiles in VS2013, but with an error in GCC with an unreadable message. VS2013 seems to be correct.

  • I do not copy the stream, but create it in place;
  • When vectorit becomes large enough, the contents should be moved to a new area of ​​memory.

Although I could not find the right place in the standard that says something to formulate on this. Can someone quote it please?

+4
source share
2 answers

If you scroll to the end of the errors you selected clang, you will see the following:

/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/basic_ios.h:66:23: note: copy constructor of 'basic_ios<char, std::char_traits<char> >' is implicitly deleted because base class 'std::ios_base' has an inaccessible copy constructor

This is the corresponding line from the long list of gcc errors:

/usr/include/c++/4.8/bits/basic_ios.h:66:11: note: 'std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)' is implicitly deleted because the default definition would be ill-formed:

     class basic_ios : public ios_base

, libstd++ basic_ios, .

27,5 | Iostreams | | basic_ios.

bugzilla. clang, lib++.

, , :

#include <sstream>
#include <utility>
std::stringstream getss(){
   std::stringstream q;
   return std::move(q);
}
+14

:

template<typename T, typename...Args>
std::unique_ptr<T> make_unique(Args&&...args) {
  return std::unique_ptr<T>( new T(std::forward<Args>(args)...) );
}

ofstream:

 template<typename...Args>
 std::unique_ptr<std::ofstream> make_up_ofstream(Args&&...args) {
   return make_unique<std::ofstream>(std::forward<Args>(args)...);
 }

:

std::vector<std::unique_ptr<std::ofstream>> v;
v.emplace_back(make_up_ofstream("file1.txt"));
v.emplace_back(make_up_ofstream("file2.txt"));
for (int i = 0, ilen = v.size(); i < ilen; ++i)
  *(v[i]) << "Test" << i << endl;

, ?

make_up, unique_ptr, :

// dense boilerplate, obsolete in C++1y:
template<unsigned...>struct indexes{typedef indexes type;};
template<unsigned Max,unsigned...Is>struct make_indexes:make_indexes<Max-1,Max-1,Is...>{};
template<unsigned...Is>struct make_indexes<0,Is...>:indexes<Is...>{};
template<unsigned Max>using make_indexes_t=typename make_indexes<Max>::type;

template<typename T>using type=T;

template<typename... Args>
struct up_maker {
  std::tuple<Args...> args;
  template<class T, class...Ds, unsigned...Is>
  std::unique_ptr<T,Ds...> helper( indexes<Is...> ) && {
    return std::unique_ptr<T,Ds...>( new T(std::forward<Args>( std::get<Is>(args) )...) );
  }
  template<class T, class...Ds>
  operator type<std::unique_ptr<T,Ds...>>() && {
    return std::move(*this).helper<T,Ds...>( make_indexes_t< sizeof...(Args) >{} );
  }
  explicit up_maker( Args&&... args_in ):args( std::forward<Args>(args_in)... ) {}
  up_maker( up_maker const& ) = delete;
  up_maker( up_maker && ) = default;
  up_maker& operator=( up_maker const& ) = delete;
  up_maker& operator=( up_maker && ) = default;
};

template<typename...Args>
up_maker<Args...> make_up( Args&&... args ) {
  return up_maker<Args...>( std::forward<Args>(args)... );
}

, , :

std::vector<std::unique_ptr<std::ofstream>> v;
v.emplace_back(make_up("file1.txt"));
v.emplace_back(make_up("file2.txt"));
for (int i = 0, ilen = v.size(); i < ilen; ++i)
  (*v[i]) << "Test" << i << std::endl;

... , _ofstream, .

+5

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


All Articles