Skip to content

Instantly share code, notes, and snippets.

@jefftrull
Created July 25, 2012 20:58
Show Gist options
  • Save jefftrull/3178642 to your computer and use it in GitHub Desktop.
Save jefftrull/3178642 to your computer and use it in GitHub Desktop.
accessing all values from a map when stored objects are non-copyable
#include <iostream>
#include <memory>
#include <boost/thread.hpp>
#include <thread>
#include <map>
#include <vector>
typedef std::map<std::size_t, boost::thread> ThreadContainer;
// this one produces the same error:
// typedef std::vector<std::pair<std::size_t, boost::thread> > ThreadContainer;
// these do not:
// typedef std::map<std::size_t, std::unique_ptr<int> > ThreadContainer;
// typedef std::map<std::size_t, std::thread> ThreadContainer;
int main() {
ThreadContainer threads;
for (auto iter = threads.begin(); iter != threads.end(); ++iter) {
std::cout << "got a thread\n";
// iter->second.join();
}
}
@jefftrull
Copy link
Author

In file included from /usr/include/c++/4.6/bits/stl_algobase.h:65:0,
from /usr/include/c++/4.6/bits/char_traits.h:41,
from /usr/include/c++/4.6/ios:41,
from /usr/include/c++/4.6/ostream:40,
from /usr/include/c++/4.6/iostream:40,
from test.cpp:1:
/usr/include/c++/4.6/bits/stl_pair.h: In instantiation of ‘std::pair<const long unsigned int, boost::thread>’:
test.cpp:10:57: instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:112:17: error: ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const long unsigned int, _T2 = boost::thread, std::pair<_T1, _T2> = std::pair<const long unsigned int, boost::thread>]’ declared to take const reference, but implicit declaration would take non-const

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment