title: Storing non copyable object with no default constructor in map (C++11)
body: <p>I'm trying to implement a class that represents a connection to a file, hence it should be a non-copyable class. Also, since a filename is required to create the object, I'd like to remove the default constructor.</p>
  
  <p>Here is a simplified definition of such a class :</p>
  
  <pre><code>class Elem
  {
  public:
      Elem() = delete;
      Elem(string name) : file(new ifstream(name,ios::in)) {}
  
      Elem(const Elem&amp;) = delete;
      Elem&amp; operator=(const Elem&amp; o) = delete;
  
      Elem&amp; operator=(Elem &amp;&amp; o)
      {
          swap(o.file,file);
          o.file = nullptr;
      }
      Elem(Elem &amp;&amp;o) : file(nullptr)
      {
          swap(file,o.file);
      }
  
      ~Elem()
      {
          if(file!=nullptr){
              file-&gt;close();
              delete file;
          }
      }
  protected:
      ifstream* file;
  };
  </code></pre>
  
  <p>However, when I try to store such objects in a standard map, the code fails to compile :</p>
  
  <pre><code>int main(){
      map&lt;string,Elem&gt; m;
      m["file1"] = move(Elem("file1"));
      cout&lt;&lt;m.size()&lt;&lt;endl;
      return 0;
  }
  </code></pre>
  
  <p>I get the following errors (gcc 4.9.1) :</p>
  
  <pre><code>In file included from /usr/include/c++/4.9.1/bits/stl_map.h:63:0,
                   from /usr/include/c++/4.9.1/map:61,
                   from test.cpp:1:
  /usr/include/c++/4.9.1/tuple: In instantiation of ‘std::pair&lt;_T1, _T2&gt;::pair(std::tuple&lt;_Args1 ...&gt;&amp;, std::tuple&lt;_Args2 ...&gt;&amp;, std::_Index_tuple&lt;_Indexes1 ...&gt;, std::_Index_tuple&lt;_Indexes2 ...&gt;) [with _Args1 = {std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;&amp;}; unsigned int ..._Indexes1 = {0u}; _Args2 = {}; unsigned int ..._Indexes2 = {}; _T1 = const std::basic_string&lt;char&gt;; _T2 = Elem]’
  /usr/include/c++/4.9.1/tuple:1088:63:   required from ‘std::pair&lt;_T1, _T2&gt;::pair(std::piecewise_construct_t, std::tuple&lt;_Args1 ...&gt;, std::tuple&lt;_Args2 ...&gt;) [with _Args1 = {std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;&amp;}; _Args2 = {}; _T1 = const std::basic_string&lt;char&gt;; _T2 = Elem]’
  /usr/include/c++/4.9.1/ext/new_allocator.h:120:4:   required from ‘void __gnu_cxx::new_allocator&lt;_Tp&gt;::construct(_Up*, _Args&amp;&amp; ...) [with _Up = std::pair&lt;const std::basic_string&lt;char&gt;, Elem&gt;; _Args = {const std::piecewise_construct_t&amp;, std::tuple&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;&amp;&gt;, std::tuple&lt;&gt;}; _Tp = std::_Rb_tree_node&lt;std::pair&lt;const std::basic_string&lt;char&gt;, Elem&gt; &gt;]’
  /usr/include/c++/4.9.1/bits/alloc_traits.h:253:4:   required from ‘static std::_Require&lt;typename std::allocator_traits&lt;_Alloc&gt;::__construct_helper&lt;_Tp, _Args&gt;::type&gt; std::allocator_traits&lt;_Alloc&gt;::_S_construct(_Alloc&amp;, _Tp*, _Args&amp;&amp; ...) [with _Tp = std::pair&lt;const std::basic_string&lt;char&gt;, Elem&gt;; _Args = {const std::piecewise_construct_t&amp;, std::tuple&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;&amp;&gt;, std::tuple&lt;&gt;}; _Alloc = std::allocator&lt;std::_Rb_tree_node&lt;std::pair&lt;const std::basic_string&lt;char&gt;, Elem&gt; &gt; &gt;; std::_Require&lt;typename std::allocator_traits&lt;_Alloc&gt;::__construct_helper&lt;_Tp, _Args&gt;::type&gt; = void]’
  /usr/include/c++/4.9.1/bits/alloc_traits.h:399:57:   required from ‘static decltype (_S_construct(__a, __p, (forward&lt;_Args&gt;)(std::allocator_traits::construct::__args)...)) std::allocator_traits&lt;_Alloc&gt;::construct(_Alloc&amp;, _Tp*, _Args&amp;&amp; ...) [with _Tp = std::pair&lt;const std::basic_string&lt;char&gt;, Elem&gt;; _Args = {const std::piecewise_construct_t&amp;, std::tuple&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;&amp;&gt;, std::tuple&lt;&gt;}; _Alloc = std::allocator&lt;std::_Rb_tree_node&lt;std::pair&lt;const std::basic_string&lt;char&gt;, Elem&gt; &gt; &gt;; decltype (_S_construct(__a, __p, (forward&lt;_Args&gt;)(std::allocator_traits::construct::__args)...)) = &lt;type error&gt;]’
  /usr/include/c++/4.9.1/bits/stl_tree.h:423:42:   required from ‘std::_Rb_tree_node&lt;_Val&gt;* std::_Rb_tree&lt;_Key, _Val, _KeyOfValue, _Compare, _Alloc&gt;::_M_create_node(_Args&amp;&amp; ...) [with _Args = {const std::piecewise_construct_t&amp;, std::tuple&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;&amp;&gt;, std::tuple&lt;&gt;}; _Key = std::basic_string&lt;char&gt;; _Val = std::pair&lt;const std::basic_string&lt;char&gt;, Elem&gt;; _KeyOfValue = std::_Select1st&lt;std::pair&lt;const std::basic_string&lt;char&gt;, Elem&gt; &gt;; _Compare = std::less&lt;std::basic_string&lt;char&gt; &gt;; _Alloc = std::allocator&lt;std::pair&lt;const std::basic_string&lt;char&gt;, Elem&gt; &gt;; std::_Rb_tree&lt;_Key, _Val, _KeyOfValue, _Compare, _Alloc&gt;::_Link_type = std::_Rb_tree_node&lt;std::pair&lt;const std::basic_string&lt;char&gt;, Elem&gt; &gt;*]’
  /usr/include/c++/4.9.1/bits/stl_tree.h:1790:64:   required from ‘std::_Rb_tree&lt;_Key, _Val, _KeyOfValue, _Compare, _Alloc&gt;::iterator std::_Rb_tree&lt;_Key, _Val, _KeyOfValue, _Compare, _Alloc&gt;::_M_emplace_hint_unique(std::_Rb_tree&lt;_Key, _Val, _KeyOfValue, _Compare, _Alloc&gt;::const_iterator, _Args&amp;&amp; ...) [with _Args = {const std::piecewise_construct_t&amp;, std::tuple&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;&amp;&amp;&gt;, std::tuple&lt;&gt;}; _Key = std::basic_string&lt;char&gt;; _Val = std::pair&lt;const std::basic_string&lt;char&gt;, Elem&gt;; _KeyOfValue = std::_Select1st&lt;std::pair&lt;const std::basic_string&lt;char&gt;, Elem&gt; &gt;; _Compare = std::less&lt;std::basic_string&lt;char&gt; &gt;; _Alloc = std::allocator&lt;std::pair&lt;const std::basic_string&lt;char&gt;, Elem&gt; &gt;; std::_Rb_tree&lt;_Key, _Val, _KeyOfValue, _Compare, _Alloc&gt;::iterator = std::_Rb_tree_iterator&lt;std::pair&lt;const std::basic_string&lt;char&gt;, Elem&gt; &gt;; std::_Rb_tree&lt;_Key, _Val, _KeyOfValue, _Compare, _Alloc&gt;::const_iterator = std::_Rb_tree_const_iterator&lt;std::pair&lt;const std::basic_string&lt;char&gt;, Elem&gt; &gt;]’
  /usr/include/c++/4.9.1/bits/stl_map.h:519:8:   required from ‘std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::mapped_type&amp; std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::operator[](std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::key_type&amp;&amp;) [with _Key = std::basic_string&lt;char&gt;; _Tp = Elem; _Compare = std::less&lt;std::basic_string&lt;char&gt; &gt;; _Alloc = std::allocator&lt;std::pair&lt;const std::basic_string&lt;char&gt;, Elem&gt; &gt;; std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::mapped_type = Elem; std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::key_type = std::basic_string&lt;char&gt;]’
  test.cpp:41:14:   required from here
  /usr/include/c++/4.9.1/tuple:1099:70: erreur: use of deleted function ‘Elem::Elem()’
           second(std::forward&lt;_Args2&gt;(std::get&lt;_Indexes2&gt;(__tuple2))...)
                                                                    ^
  test.cpp:11:5: note: declared here
       Elem() = delete;
  </code></pre>
  
  <p>Any clues about how this could be achieved ? Are their other design flaws in my structure ?</p>
  
  <p>Thanks in advance</p>