Take edmonds_maximum_cardinality_matching for example. I can do:
vector<uint32_t> mate_map(n_vertices);
edmonds_maximum_cardinality_matching(g, &mate_map[0]);
But not:
vector<uint32_t> mate_map(n_vertices);
edmonds_maximum_cardinality_matching(g, mate_map);
Why is that so? A vector has all operations an array has right? NB: Here is the error message (for the second snippet) which I could not really handle:
In file included from main.C:5:0:
/usr/include/boost/graph/max_cardinality_matching.hpp: In instantiation of ‘static void boost::extra_greedy_matching<Graph, MateMap>::find_matching(const Graph&, MateMap) [with Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS>; MateMap = std::vector<unsigned int>]’:
/usr/include/boost/graph/max_cardinality_matching.hpp:842:63: required from ‘bool boost::matching(const Graph&, MateMap, VertexIndexMap) [with Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS>; MateMap = std::vector<unsigned int>; VertexIndexMap = boost::vec_adj_list_vertex_id_map<boost::no_property, long unsigned int>; AugmentingPathFinder = boost::edmonds_augmenting_path_finder; InitialMatchingFinder = boost::extra_greedy_matching; MatchingVerifier = boost::no_matching_verifier]’
/usr/include/boost/graph/max_cardinality_matching.hpp:885:19: required from ‘void boost::edmonds_maximum_cardinality_matching(const Graph&, MateMap, VertexIndexMap) [with Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS>; MateMap = std::vector<unsigned int>; VertexIndexMap = boost::vec_adj_list_vertex_id_map<boost::no_property, long unsigned int>]’
/usr/include/boost/graph/max_cardinality_matching.hpp:894:70: required from ‘void boost::edmonds_maximum_cardinality_matching(const Graph&, MateMap) [with Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS>; MateMap = std::vector<unsigned int>]’
main.C:20:51: required from here
/usr/include/boost/graph/max_cardinality_matching.hpp:617:58: error: no matching function for call to ‘put(std::vector<unsigned int>&, boost::iterators::detail::iterator_facade_base<boost::range_detail::integer_iterator<long unsigned int>, long unsigned int, boost::iterators::random_access_traversal_tag, long unsigned int, long int, false, false>::reference, boost::graph_traits<boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS> >::vertex_descriptor)’
put(mate, *vi, graph_traits<Graph>::null_vertex());
^
/usr/include/boost/graph/max_cardinality_matching.hpp:617:58: note: candidates are:
In file included from /usr/include/boost/graph/adjacency_list.hpp:36:0,
from main.C:4:
/usr/include/boost/property_map/property_map.hpp:124:15: note: template<class T, class V> void put(T*, std::ptrdiff_t, const V&)
inline void put(T* pa, std::ptrdiff_t k, const V& val) { pa[k] = val; }
^
/usr/include/boost/property_map/property_map.hpp:124:15: note: template argument deduction/substitution failed:
In file included from main.C:5:0:
/usr/include/boost/graph/max_cardinality_matching.hpp:617:58: note: mismatched types ‘T*’ and ‘std::vector<unsigned int>’
put(mate, *vi, graph_traits<Graph>::null_vertex());
^
In file included from /usr/include/boost/graph/adjacency_list.hpp:36:0,
from main.C:4:
/usr/include/boost/property_map/property_map.hpp:193:8: note: template<class K, class V> void boost::put(const boost::writable_property_map_archetype<K, V>&, const typename boost::writable_property_map_archetype<K, V>::key_type&, const typename boost::writable_property_map_archetype<K, V>::value_type&)
void put(const writable_property_map_archetype<K,V>&,
^
/usr/include/boost/property_map/property_map.hpp:193:8: note: template argument deduction/substitution failed:
In file included from main.C:5:0:
/usr/include/boost/graph/max_cardinality_matching.hpp:617:58: note: ‘std::vector<unsigned int>’ is not derived from ‘const boost::writable_property_map_archetype<K, V>’
put(mate, *vi, graph_traits<Graph>::null_vertex());
^
In file included from /usr/include/boost/graph/adjacency_list.hpp:36:0,
from main.C:4:
/usr/include/boost/property_map/property_map.hpp:307:3: note: template<class PropertyMap, class Reference, class K, class V> void boost::put(const boost::put_get_helper<Reference, PropertyMap>&, K, const V&)
put(const put_get_helper<Reference, PropertyMap>& pa, K k, const V& v)
^
/usr/include/boost/property_map/property_map.hpp:307:3: note: template argument deduction/substitution failed:
In file included from main.C:5:0:
/usr/include/boost/graph/max_cardinality_matching.hpp:617:58: note: ‘std::vector<unsigned int>’ is not derived from ‘const boost::put_get_helper<Reference, PropertyMap>’
put(mate, *vi, graph_traits<Graph>::null_vertex());
^
In file included from /usr/include/boost/graph/properties.hpp:21:0,
from /usr/include/boost/graph/adjacency_list.hpp:44,
from main.C:4:
/usr/include/boost/graph/property_maps/null_property_map.hpp:32:10: note: template<class K, class V> void boost::put(boost::null_property_map<K, V>&, const K&, const V&)
void put(null_property_map<K,V>& /*pm*/, const K& /*key*/, const V& /*value*/)
^
/usr/include/boost/graph/property_maps/null_property_map.hpp:32:10: note: template argument deduction/substitution failed:
In file included from main.C:5:0:
/usr/include/boost/graph/max_cardinality_matching.hpp:617:58: note: ‘std::vector<unsigned int>’ is not derived from ‘boost::null_property_map<K, V>’
put(mate, *vi, graph_traits<Graph>::null_vertex());
^
In file included from /usr/include/boost/graph/adjacency_list.hpp:246:0,
from main.C:4:
/usr/include/boost/graph/detail/adjacency_list.hpp:1760:5: note: template<class Config, class Base, class Property, class Key, class Value> void boost::put(Property, boost::adj_list_helper<Config, Base>&, const Key&, const Value&)
put(Property p, adj_list_helper<Config, Base>& g,
^
/usr/include/boost/graph/detail/adjacency_list.hpp:1760:5: note: template argument deduction/substitution failed:
In file included from main.C:5:0:
/usr/include/boost/graph/max_cardinality_matching.hpp:617:58: note: mismatched types ‘boost::adj_list_helper<Config, Base>’ and ‘boost::iterators::detail::iterator_facade_base<boost::range_detail::integer_iterator<long unsigned int>, long unsigned int, boost::iterators::random_access_traversal_tag, long unsigned int, long int, false, false>::reference {aka long unsigned int}’
put(mate, *vi, graph_traits<Graph>::null_vertex());
^
In file included from /usr/include/boost/graph/max_cardinality_matching.hpp:22:0,
from main.C:5:
/usr/include/boost/graph/filtered_graph.hpp:472:3: note: template<class G, class EP, class VP, class Property, class Key, class Value> void boost::put(Property, const boost::filtered_graph<Graph, EdgePredicate, VertexPredicate>&, const Key&, const Value&)
put(Property p, const filtered_graph<G, EP, VP>& g, const Key& k,
^
/usr/include/boost/graph/filtered_graph.hpp:472:3: note: template argument deduction/substitution failed:
In file included from main.C:5:0:
/usr/include/boost/graph/max_cardinality_matching.hpp:617:58: note: mismatched types ‘const boost::filtered_graph<Graph, EdgePredicate, VertexPredicate>’ and ‘boost::iterators::detail::iterator_facade_base<boost::range_detail::integer_iterator<long unsigned int>, long unsigned int, boost::iterators::random_access_traversal_tag, long unsigned int, long int, false, false>::reference {aka long unsigned int}’
put(mate, *vi, graph_traits<Graph>::null_vertex());
^
Full output here: http://pastebin.com/fG2aLRiG (Outsourced because of SO's restrictions on post length)
"A vector has all operations an array has" No. Arrays implicitly decay to pointers, vector does not. So when boost tries to pass it to the put function it generates this error:
error: no matching function for call to ‘put(std::vector<unsigned int>&, [blah blah])
note: candidates are:
template<class T, class V> void put(T*, std::ptrdiff_t, const V&)
template argument deduction/substitution failed:
mismatched types ‘T*’ and ‘std::vector<unsigned int>’
[followed by details about the other potential put functions]
Apperently the put function would accept one of these, or something that implicitly converts to one of these.
T*
boost::writable_property_map_archetype<K, V>&
const boost::put_get_helper<Reference, PropertyMap>&
boost::null_property_map<K, V>&
Property
(edmonds_maximum_cardinality_matching may have other requirements that cause these other types to also be invalid, I'm just saying these five types pass this particular hurdle.)
No, a std::vector does not have all these operations and won't convert to an array implicitly. Ignoring the fact that a std::array won't change that, for your purposes, it would be better suited.
Related
I'm learning some C++ features by implementing an octree class. I want the subscript operator on this class to return the octant corresponding to an index. How should I define the subscript operator on the class so that I can both (i) assign to the result and (ii) check if the result is empty or not?
Goal (i) is idiomatically achieved by making the subscript operator return a reference. But references can't refer to nothing, which is a legitimate return value if an octant is empty.
Goal (ii) can be achieved by making the subscript operator return an optional. But then it becomes non-trivial to modify the pointer to the octant – with std::optional<T>, T cannot be a reference.
Here is the broken example (except all instances of optional are prefixed by experimental in the real code because my GCC only has experimental support for C++17).
#include <optional>
#include <iostream>
using namespace std;
class Octree {
Octree* branch[8];
public:
Octree();
~Octree();
optional<Octree&> operator[](int index);
};
Octree::Octree() : branch{}
{
}
Octree::~Octree()
{
for (int i = 0; i < 8; i++) {
if (branch[i])
delete branch[i];
}
}
optional<Octree&> Octree::operator[](int index)
{
if (branch[index] == NULL)
return nullopt;
else
return &branch[index];
}
int main(int argc, char *argv[])
{
Octree o;
if (o[0])
cout << "Octant o[0] is not empty.\n";
else
cout << "Octant o[0] is empty.\n";
o[0] = new Octree(); // The intent is to modify o
return 0;
}
The compiler, as expected, colorfully rejects an optional reference.
In file included from parc.cpp:1:0:
/usr/include/c++/6/experimental/optional: In instantiation of ‘class std::experimental::fundamentals_v1::optional<Octree&>’:
parc.cpp:26:61: required from here
/usr/include/c++/6/experimental/optional:507:7: error: static assertion failed: Invalid instantiation of optional<T>
static_assert(__and_<__not_<is_same<remove_cv_t<_Tp>, nullopt_t>>,
^~~~~~~~~~~~~
/usr/include/c++/6/experimental/optional:713:7: error: forming pointer to reference type ‘Octree&’
operator->() const
^~~~~~~~
/usr/include/c++/6/experimental/optional:723:7: error: forming pointer to reference type ‘Octree&’
operator->()
^~~~~~~~
parc.cpp: In member function ‘std::experimental::fundamentals_v1::optional<Octree&> Octree::operator[](int)’:
parc.cpp:31:10: error: could not convert ‘&((Octree*)this)->Octree::branch[index]’ from ‘Octree**’ to ‘std::experimental::fundamentals_v1::optional<Octree&>’
return &branch[index];
^~~~~~~~~~~~~~
parc.cpp: In function ‘int main(int, char**)’:
parc.cpp:41:24: error: no match for ‘operator=’ (operand types are ‘std::experimental::fundamentals_v1::optional<Octree&>’ and ‘Octree*’)
o[0] = new Octree();
^
In file included from parc.cpp:1:0:
/usr/include/c++/6/experimental/optional:595:7: note: candidate: std::experimental::fundamentals_v1::optional<_Tp>& std::experimental::fundamentals_v1::optional<_Tp>::operator=(std::experimental::fundamentals_v1::nullopt_t) [with _Tp = Octree&]
operator=(nullopt_t) noexcept
^~~~~~~~
/usr/include/c++/6/experimental/optional:595:7: note: no known conversion for argument 1 from ‘Octree*’ to ‘std::experimental::fundamentals_v1::nullopt_t’
/usr/include/c++/6/experimental/optional:609:9: note: candidate: template<class _Up> std::enable_if_t<std::__and_<std::__not_<std::is_same<std::experimental::fundamentals_v1::optional<_Tp>, typename std::decay<_Up>::type> >, std::is_constructible<_Tp, _Up>, std::__not_<std::__and_<std::is_scalar<_Tp>, std::is_same<_Tp, typename std::decay<_Up>::type> > >, std::is_assignable<_Tp&, _Up> >::value, std::experimental::fundamentals_v1::optional<_Tp>&> std::experimental::fundamentals_v1::optional<_Tp>::operator=(_Up&&) [with _Up = _Up; _Tp = Octree&]
operator=(_Up&& __u)
^~~~~~~~
/usr/include/c++/6/experimental/optional:609:9: note: template argument deduction/substitution failed:
/usr/include/c++/6/experimental/optional:628:9: note: candidate: template<class _Up> std::enable_if_t<std::__and_<std::__not_<std::is_same<_T1, _U1> >, std::is_constructible<_Tp, const _Up&>, std::is_assignable<_Tp&, _Up>, std::__not_<std::__or_<std::is_constructible<_Tp, const std::experimental::fundamentals_v1::optional<_Up>&>, std::is_constructible<_Tp, std::experimental::fundamentals_v1::optional<_Up>&>, std::is_constructible<_Tp, const std::experimental::fundamentals_v1::optional<_Up>&&>, std::is_constructible<_Tp, std::experimental::fundamentals_v1::optional<_Up>&&>, std::is_convertible<const std::experimental::fundamentals_v1::optional<_Up>&, _Tp>, std::is_convertible<std::experimental::fundamentals_v1::optional<_Up>&, _Tp>, std::is_convertible<const std::experimental::fundamentals_v1::optional<_Up>&&, _Tp>, std::is_convertible<std::experimental::fundamentals_v1::optional<_Up>&&, _Tp> > >, std::__not_<std::__or_<std::is_assignable<_Tp&, const std::experimental::fundamentals_v1::optional<_Up>&>, std::is_assignable<_Tp&, std::experimental::fundamentals_v1::optional<_Up>&>, std::is_assignable<_Tp&, const std::experimental::fundamentals_v1::optional<_Up>&&>, std::is_assignable<_Tp&, std::experimental::fundamentals_v1::optional<_Up>&&> > > >::value, std::experimental::fundamentals_v1::optional<_Tp>&> std::experimental::fundamentals_v1::optional<_Tp>::operator=(const std::experimental::fundamentals_v1::optional<_Up>&) [with _Up = _Up; _Tp = Octree&]
operator=(const optional<_Up>& __u)
^~~~~~~~
/usr/include/c++/6/experimental/optional:628:9: note: template argument deduction/substitution failed:
parc.cpp:41:24: note: mismatched types ‘const std::experimental::fundamentals_v1::optional<_Tp>’ and ‘Octree*’
o[0] = new Octree();
^
In file included from parc.cpp:1:0:
/usr/include/c++/6/experimental/optional:653:9: note: candidate: template<class _Up> std::enable_if_t<std::__and_<std::__not_<std::is_same<_T1, _U1> >, std::is_constructible<_Tp, _Up>, std::is_assignable<_Tp&, _Up>, std::__not_<std::__or_<std::is_constructible<_Tp, const std::experimental::fundamentals_v1::optional<_Up>&>, std::is_constructible<_Tp, std::experimental::fundamentals_v1::optional<_Up>&>, std::is_constructible<_Tp, const std::experimental::fundamentals_v1::optional<_Up>&&>, std::is_constructible<_Tp, std::experimental::fundamentals_v1::optional<_Up>&&>, std::is_convertible<const std::experimental::fundamentals_v1::optional<_Up>&, _Tp>, std::is_convertible<std::experimental::fundamentals_v1::optional<_Up>&, _Tp>, std::is_convertible<const std::experimental::fundamentals_v1::optional<_Up>&&, _Tp>, std::is_convertible<std::experimental::fundamentals_v1::optional<_Up>&&, _Tp> > >, std::__not_<std::__or_<std::is_assignable<_Tp&, const std::experimental::fundamentals_v1::optional<_Up>&>, std::is_assignable<_Tp&, std::experimental::fundamentals_v1::optional<_Up>&>, std::is_assignable<_Tp&, const std::experimental::fundamentals_v1::optional<_Up>&&>, std::is_assignable<_Tp&, std::experimental::fundamentals_v1::optional<_Up>&&> > > >::value, std::experimental::fundamentals_v1::optional<_Tp>&> std::experimental::fundamentals_v1::optional<_Tp>::operator=(std::experimental::fundamentals_v1::optional<_Up>&&) [with _Up = _Up; _Tp = Octree&]
operator=(optional<_Up>&& __u)
^~~~~~~~
/usr/include/c++/6/experimental/optional:653:9: note: template argument deduction/substitution failed:
parc.cpp:41:24: note: mismatched types ‘std::experimental::fundamentals_v1::optional<_Tp>’ and ‘Octree*’
o[0] = new Octree();
^
In file included from parc.cpp:1:0:
/usr/include/c++/6/experimental/optional:493:11: note: candidate: std::experimental::fundamentals_v1::optional<Octree&>& std::experimental::fundamentals_v1::optional<Octree&>::operator=(const std::experimental::fundamentals_v1::optional<Octree&>&)
class optional
^~~~~~~~
/usr/include/c++/6/experimental/optional:493:11: note: no known conversion for argument 1 from ‘Octree*’ to ‘const std::experimental::fundamentals_v1::optional<Octree&>&’
/usr/include/c++/6/experimental/optional:493:11: note: candidate: std::experimental::fundamentals_v1::optional<Octree&>& std::experimental::fundamentals_v1::optional<Octree&>::operator=(std::experimental::fundamentals_v1::optional<Octree&>&&)
/usr/include/c++/6/experimental/optional:493:11: note: no known conversion for argument 1 from ‘Octree*’ to ‘std::experimental::fundamentals_v1::optional<Octree&>&&’
/usr/include/c++/6/experimental/optional: In instantiation of ‘void std::experimental::fundamentals_v1::_Optional_base<_Tp, false>::_M_construct(_Args&& ...) [with _Args = {Octree}; _Tp = Octree&]’:
/usr/include/c++/6/experimental/optional:384:11: required from ‘std::experimental::fundamentals_v1::_Optional_base<_Tp, false>::_Optional_base(std::experimental::fundamentals_v1::_Optional_base<_Tp, false>&&) [with _Tp = Octree&]’
/usr/include/c++/6/experimental/optional:493:11: required from here
/usr/include/c++/6/experimental/optional:439:11: error: new cannot be applied to a reference type
::new (std::__addressof(this->_M_payload))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_Stored_type(std::forward<_Args>(__args)...);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I'm sure there's a way of overloading assignment so that I can return an optional and then assign to it like I do in main above. Thanks for any pointers! ;-)
Goal(i) can be achieved by returning a helper class that overloads the = operator. Goal(ii) can be achieved by returning a helper class that overloads the bool operator.
Consider what happens when your operator[] returns a class that looks like this:
class Octree {
// Other declarations...
public:
// Other declarations...
struct value_at {
Octree *ptr;
operator bool() const { return ptr != nullptr; }
Octree &operator=(const Octree &v)
{
return *ptr=v;
}
};
value_at operator[](int index);
};
Constructing the value_at would be your homework assignment; but it's obvious that a returned object with a null ptr represents a nonexistent value, otherwise it points to the value being returned.
Now, your [] operator returns can be used in a boolean context, which evaluates to an indication of whether or not a value was returned, and assigning something to the returned value ends up assigning to the value that [] supposedly returned.
The = operator overload can also check if ptr is null, and throw an exception, as a debugging aid.
The helper class can also declare an operator Octree() const overload, so that the returned object appears to be even more transparent.
Having said all of the above: you could also return a std::optional<std::reference_wrapper<Octree>> which is actually closer aligned to the object described in your question. However using it, in practice, may prove to require some cumbersome syntax (assigning to such std::optional may not necessarily have the effect you're looking for). A simple helper class like this usually leads to a more natural, transparent usage.
Wrote short example: simple echo program. Please, help to compile this, because I don't understand why compiler error
I tried this: g++ -Wall -Wextra -pedantic client.cpp -o client -lboost_thread -lboost_system, but got next error https://pastebin.com/byevYxPa
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <string>
using namespace boost::asio;
using error_code = boost::system::error_code;
io_service ioservice;
posix::stream_descriptor out(ioservice, STDOUT_FILENO);
posix::stream_descriptor in(ioservice, STDIN_FILENO);
std::string line;
void on_read(const error_code & err, std::size_t bytes);
void on_write(const error_code & err, std::size_t bytes);
void on_read(const error_code & err, std::size_t bytes) {
if (err || line == "exit") return;
line += "\n";
async_write(out, buffer(line), on_write);
}
void on_write(const error_code & err, std::size_t bytes) {
write(out, buffer("$ "));
async_read_until(in, buffer(line),'\n',on_read);
}
int main() {
async_read_until(in, buffer(line),'\n',on_read);
ioservice.run();
}
I expected that this code will works correctly and will be compiled
client.cpp: In function ‘void on_read(const error_code&, std::size_t)’:
client.cpp:17:50: warning: unused parameter ‘bytes’ [-Wunused-parameter]
void on_read(const error_code & err, std::size_t bytes) {
^~~~~
client.cpp: In function ‘void on_write(const error_code&, std::size_t)’:
client.cpp:25:51: error: no matching function for call to ‘async_read_until(boost::asio::posix::stream_descriptor&, boost::asio::const_buffers_1, char, void (&)(const error_code&, std::size_t))’
async_read_until(in, buffer(line),'\n',on_read);
^
In file included from /usr/include/boost/asio/read_until.hpp:921:0,
from /usr/include/boost/asio.hpp:91,
from client.cpp:1:
/usr/include/boost/asio/impl/read_until.hpp:498:1: note: candidate: template<class AsyncReadStream, class Allocator, class ReadHandler> typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, char, ReadHandler&&)
async_read_until(AsyncReadStream& s,
^~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/read_until.hpp:498:1: note: template argument deduction/substitution failed:
client.cpp:25:51: note: ‘boost::asio::const_buffers_1’ is not derived from ‘boost::asio::basic_streambuf<Allocator>’
async_read_until(in, buffer(line),'\n',on_read);
^
In file included from /usr/include/boost/asio/read_until.hpp:921:0,
from /usr/include/boost/asio.hpp:91,
from client.cpp:1:
/usr/include/boost/asio/impl/read_until.hpp:701:1: note: candidate: template<class AsyncReadStream, class Allocator, class ReadHandler> typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, const string&, ReadHandler&&)
async_read_until(AsyncReadStream& s,
^~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/read_until.hpp:701:1: note: template argument deduction/substitution failed:
client.cpp:25:51: note: ‘boost::asio::const_buffers_1’ is not derived from ‘boost::asio::basic_streambuf<Allocator>’
async_read_until(in, buffer(line),'\n',on_read);
^
In file included from /usr/include/boost/asio/read_until.hpp:921:0,
from /usr/include/boost/asio.hpp:91,
from client.cpp:1:
/usr/include/boost/asio/impl/read_until.hpp:913:1: note: candidate: template<class AsyncReadStream, class Allocator, class ReadHandler> typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, const regex&, ReadHandler&&)
async_read_until(AsyncReadStream& s,
^~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/read_until.hpp:913:1: note: template argument deduction/substitution failed:
client.cpp:25:51: note: ‘boost::asio::const_buffers_1’ is not derived from ‘boost::asio::basic_streambuf<Allocator>’
async_read_until(in, buffer(line),'\n',on_read);
^
In file included from /usr/include/boost/asio/read_until.hpp:921:0,
from /usr/include/boost/asio.hpp:91,
from client.cpp:1:
/usr/include/boost/asio/impl/read_until.hpp:1122:1: note: candidate: template<class AsyncReadStream, class Allocator, class MatchCondition, class ReadHandler> typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, MatchCondition, ReadHandler&&, typename std::enable_if<boost::asio::is_match_condition<MatchCondition>::value>::type*)
async_read_until(AsyncReadStream& s,
^~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/read_until.hpp:1122:1: note: template argument deduction/substitution failed:
client.cpp:25:51: note: ‘boost::asio::const_buffers_1’ is not derived from ‘boost::asio::basic_streambuf<Allocator>’
async_read_until(in, buffer(line),'\n',on_read);
^
client.cpp:23:34: warning: unused parameter ‘err’ [-Wunused-parameter]
void on_write(const error_code & err, std::size_t bytes) {
^~~
client.cpp:23:51: warning: unused parameter ‘bytes’ [-Wunused-parameter]
void on_write(const error_code & err, std::size_t bytes) {
^~~~~
client.cpp: In function ‘int main()’:
client.cpp:29:51: error: no matching function for call to ‘async_read_until(boost::asio::posix::stream_descriptor&, boost::asio::const_buffers_1, char, void (&)(const error_code&, std::size_t))’
async_read_until(in, buffer(line),'\n',on_read);
^
In file included from /usr/include/boost/asio/read_until.hpp:921:0,
from /usr/include/boost/asio.hpp:91,
from client.cpp:1:
/usr/include/boost/asio/impl/read_until.hpp:498:1: note: candidate: template<class AsyncReadStream, class Allocator, class ReadHandler> typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, char, ReadHandler&&)
async_read_until(AsyncReadStream& s,
^~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/read_until.hpp:498:1: note: template argument deduction/substitution failed:
client.cpp:29:51: note: ‘boost::asio::const_buffers_1’ is not derived from ‘boost::asio::basic_streambuf<Allocator>’
async_read_until(in, buffer(line),'\n',on_read);
^
In file included from /usr/include/boost/asio/read_until.hpp:921:0,
from /usr/include/boost/asio.hpp:91,
from client.cpp:1:
/usr/include/boost/asio/impl/read_until.hpp:701:1: note: candidate: template<class AsyncReadStream, class Allocator, class ReadHandler> typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, const string&, ReadHandler&&)
async_read_until(AsyncReadStream& s,
^~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/read_until.hpp:701:1: note: template argument deduction/substitution failed:
client.cpp:29:51: note: ‘boost::asio::const_buffers_1’ is not derived from ‘boost::asio::basic_streambuf<Allocator>’
async_read_until(in, buffer(line),'\n',on_read);
^
In file included from /usr/include/boost/asio/read_until.hpp:921:0,
from /usr/include/boost/asio.hpp:91,
from client.cpp:1:
/usr/include/boost/asio/impl/read_until.hpp:913:1: note: candidate: template<class AsyncReadStream, class Allocator, class ReadHandler> typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, const regex&, ReadHandler&&)
async_read_until(AsyncReadStream& s,
^~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/read_until.hpp:913:1: note: template argument deduction/substitution failed:
client.cpp:29:51: note: ‘boost::asio::const_buffers_1’ is not derived from ‘boost::asio::basic_streambuf<Allocator>’
async_read_until(in, buffer(line),'\n',on_read);
^
In file included from /usr/include/boost/asio/read_until.hpp:921:0,
from /usr/include/boost/asio.hpp:91,
from client.cpp:1:
/usr/include/boost/asio/impl/read_until.hpp:1122:1: note: candidate: template<class AsyncReadStream, class Allocator, class MatchCondition, class ReadHandler> typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::async_read_until(AsyncReadStream&, boost::asio::basic_streambuf<Allocator>&, MatchCondition, ReadHandler&&, typename std::enable_if<boost::asio::is_match_condition<MatchCondition>::value>::type*)
async_read_until(AsyncReadStream& s,
^~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/read_until.hpp:1122:1: note: template argument deduction/substitution failed:
client.cpp:29:51: note: ‘boost::asio::const_buffers_1’ is not derived from ‘boost::asio::basic_streambuf<Allocator>’
async_read_until(in, buffer(line),'\n',on_read);
^
asio::buffer returns mutable buffer type, it doesn't match to the second param of async_read_until. This overload takes dynamic buffer, just use boost::asio::dynamic_buffer.
void on_write(const error_code & err, std::size_t bytes) {
write(out, buffer("$ "));
async_read_until(in, dynamic_buffer(line),'\n',on_read);
}
int main() {
async_read_until(in, dynamic_buffer(line),'\n',on_read);
ioservice.run();
}
LIVE demo
I am using unordered map for the first time and that with custom objects. I tried to write the below functions. There are errors that I need help with.
This is the class.
class Node
{
public:
int g= 0, h=0;
char val; //Char value in the grid
pair<int,int> pos,parent;
bool par_prsnt = false; //Bool to check if the parent is set
Node(pair<int,int>nodePos,char value)
{
pos=nodePos;
val=value;
}
int move_cost(Node other)
{
if (val=='.')
return 0;
else
return 1;
}
pair<int,int> get_pos() const
{
return pos;
}
void set_parent(pair<int,int> par)
{
parent = par;
par_prsnt = true;
}
};
Below is the custom functions:
// Custom Hasher for Class Node, to be used for Unordered_set
struct NodeHasher
{
template <typename T, typename U>
size_t
const operator()(const Node &obj)
{
pair<T,U> position;
position = obj.get_pos();
return 3* std::hash<T>()(position.first) + std::hash<U>()(position.second) ;
}
};
// Custom Comparator for Class Node, to be used for Unordered_set
struct NodeComparator
{
bool
const operator()(const Node &obj1, const Node &obj2) const
{
if (obj1.get_pos() == obj2.get_pos())
return true;
return false;
}
};
I get the following errors:
Player 1: compilation error
In file included from /usr/include/c++/7/bits/hashtable.h:35:0,
from /usr/include/c++/7/unordered_map:47,
from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:117,
solution.cc:7:
/usr/include/c++/7/bits/hashtable_policy.h: In instantiation of ‘struct std::__detail::__is_noexcept_hash’:
/usr/include/c++/7/type_traits:143:12: required from ‘struct std::__and_, std::__detail::__is_noexcept_hash >’
/usr/include/c++/7/type_traits:154:31: required from ‘struct std::__not_, std::__detail::__is_noexcept_hash > >’
/usr/include/c++/7/bits/unordered_set.h:98:63: required from ‘class std::unordered_set’
solution.cc:119:51: required from here
/usr/include/c++/7/bits/hashtable_policy.h:87:34: error: no match for call to ‘(const NodeHasher) (const Node&)’
noexcept(declval()(declval()))>
~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
solution.cc:83:9: note: candidate: template const size_t NodeHasher::operator()(const Node&)
const operator()(const Node &obj)
^~~~~~~~
solution.cc:83:9: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/7/bits/hashtable.h:35:0,
from /usr/include/c++/7/unordered_map:47,
from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:117,
solution.cc:7:
/usr/include/c++/7/bits/hashtable_policy.h:87:34: note: couldn't deduce template parameter ‘T’
noexcept(declval()(declval()))>
~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/move.h:54:0,
from /usr/include/c++/7/bits/stl_pair.h:59,
from /usr/include/c++/7/bits/stl_algobase.h:64,
from /usr/include/c++/7/vector:60,
solution.cc:3:
/usr/include/c++/7/type_traits: In instantiation of ‘struct std::__not_, std::__detail::__is_noexcept_hash > >’:
/usr/include/c++/7/bits/unordered_set.h:98:63: required from ‘class std::unordered_set’
solution.cc:119:51: required from here
/usr/include/c++/7/type_traits:154:31: error: ‘value’ is not a member of ‘std::__and_, std::__detail::__is_noexcept_hash >’
: public __bool_constant
^~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/unordered_set:48:0,
from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:118,
solution.cc:7:
/usr/include/c++/7/bits/unordered_set.h: In instantiation of ‘class std::unordered_set’:
solution.cc:119:51: required from here
/usr/include/c++/7/bits/unordered_set.h:98:63: error: ‘value’ is not a member of ‘std::__not_, std::__detail::__is_noexcept_hash > >’
typedef __uset_hashtable _Hashtable;
^~~~~~~~~~
/usr/include/c++/7/bits/unordered_set.h:105:45: error: ‘value’ is not a member of ‘std::__not_, std::__detail::__is_noexcept_hash > >’
typedef typename _Hashtable::key_type key_type;
^~~~~~~~
The error list is much longer, I think this much maybe enough for someone to understand the error. I referred this page for the custom functions : http://thispointer.com/how-to-use-unordered_set-with-user-defined-classes-tutorial-example/. Any suggestions? Thanks for reading.
Edit 1:
Unordered_set creation:
unordered_set<Node,NodeHasher,NodeComparator> openList;
Edit 2:
Error in comparison operator.
Player 1: compilation error
In file included from /usr/include/c++/7/bits/stl_algobase.h:71:0,
from /usr/include/c++/7/vector:60,
solution.cc:3:
/usr/include/c++/7/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Iter_equals_val::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator >; _Value = const Node]’:
/usr/include/c++/7/bits/stl_algo.h:120:14: required from ‘_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val]’
/usr/include/c++/7/bits/stl_algo.h:161:23: required from ‘_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val]’
/usr/include/c++/7/bits/stl_algo.h:3907:28: required from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator >; _Tp = Node]’
solution.cc:147:73: required from here
/usr/include/c++/7/bits/predefined_ops.h:241:17: error: no match for ‘operator==’ (operand types are ‘Node’ and ‘const Node’)
{ return *__it == _M_value; }
~~~~~~^~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algobase.h:67:0,
from /usr/include/c++/7/vector:60,
solution.cc:3:
/usr/include/c++/7/bits/stl_iterator.h:859:5: note: candidate: template bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator&, const __gnu_cxx::__normal_iterator&)
operator==(const __normal_iterator& __lhs,
^~~~~~~~
/usr/include/c++/7/bits/stl_iterator.h:859:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/7/bits/stl_algobase.h:71:0,
from /usr/include/c++/7/vector:60,
solution.cc:3:
/usr/include/c++/7/bits/predefined_ops.h:241:17: note: ‘Node’ is not derived from ‘const __gnu_cxx::__normal_iterator’
{ return *__it == _M_value; }
~~~~~~^~~~~~~~~~~
In file included from /usr/include/c++/7/bits/stl_algobase.h:67:0,
from /usr/include/c++/7/vector:60,
solution.cc:3:
/usr/include/c++/7/bits/stl_iterator.h:866:5: note: candidate: template bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator&, const __gnu_cxx::__normal_iterator&)
operator==(const __normal_iterator& __lhs,
^~~~~~~~
/usr/include/c++/7/bits/stl_iterator.h:866:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/7/bits/stl_algobase.h:71:0,
from /usr/include/c++/7/vector:60,
solution.cc:3:
/usr/include/c++/7/bits/predefined_ops.h:241:17: note: ‘Node’ is not derived from ‘const __gnu_cxx::__normal_iterator’
{ return *__it == _M_value; }
~~~~~~^~~~~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/7/bits/c++allocator.h:33:0,
from /usr/include/c++/7/bits/allocator.h:46,
from /usr/include/c++/7/vector:61,
solution.cc:3:
/usr/include/c++/7/ext/new_allocator.h:155:5: note: candidate: template bool __gnu_cxx::operator==(const __gnu_cxx::new_allocator&, const __gnu_cxx::new_allocator&)
operator==(const new_allocator&, const new_allocator&)
^~~~~~~~
/usr/include/c++/7/ext/new_allocator.h:155:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/7/bits/stl_algobase.h:71:0,
from /usr/include/c++/7/vector:60,
solution.cc:3:
/usr/include/c++/7/bits/predefined_ops.h:241:17: note: ‘Node’ is not derived from ‘const __gnu_cxx::new_allocator’
{ return *__it == _M_value; }
~~~~~~^~~~~~~~~~~
solution.cc: In function ‘std::vector aStar(std::vector >&, std::pair, std::pair)’:
solution.cc:173:1: error: control reaches end of non-void function [-Werror=return-type]
}
^
cc1plus: some warnings being treated as errors
struct NodeHasher
{
template <typename T, typename U>
size_t const operator()(const Node &obj)
{
pair<T,U> position;
position = obj.get_pos();
return 3* std::hash<T>()(position.first) + std::hash<U>()(position.second) ;
}
};
The compiler can deduce what T and U are in this case. One solution would be to remove the template and hard code int since you know you are working with a pair of int.
And I believe you misplaced the const specifier -- it should be placed at the end of the declaration
Alternatively, if you want to make NodeHasher work for different types of pair, You can make it a template class instead.
template <typename T, typename U>
struct NodeHasher
{
size_t operator()(const Node &obj) const
{
pair<T, U> position = obj.get_pos();
return 3* std::hash<T>()(position.first) + std::hash<U>()(position.second) ;
}
};
And create your unordered_set as
unordered_set<Node, NodeHasher<int,int>, NodeComparator> us; //T and U are int
I'm trying to make a method which constructs a tuple from provided parameters, and 'fills' it recursively. Unfortunately, it yields compilation errors... This is the code:
template<typename Head, typename... Tail>
std::vector<IntersectionComponents<Head, Tail...>> intersection() {
std::vector<IntersectionComponents<Head, Tail...>> results;
auto& headComponents = *getAllComponents<Head>();
for (auto& headComponent : headComponents) {
IntersectionComponents<Head, Tail...> currentEntityRequiredComponents;
if (allComponentsExist<IntersectionComponents<Head, Tail...>, Tail...>(headComponent.entityID, currentEntityRequiredComponents)) {
currentEntityRequiredComponents.set(headComponent);
results.push_back(std::move(currentEntityRequiredComponents));
}
}
return results;
}
template<typename IntersectComponents, typename Head, typename... Tail>
bool allComponentsExist(EntityID entityID, IntersectComponents& components) {
auto currentComponent = getComponent<Head>(entityID);
if (!currentComponent) {
return false;
}
if (allComponentsExist<IntersectComponents, Tail...>(entityID, components)) {
components.set(currentComponent);
return true;
}
return false;
}
It's not self contained, but I've tested IntersectionComponents class and it works. Code breaks on allComponentsExist call. It's template parameters probably need to be specified some other way...
This is definition of IntersectionComponents class, in case it would be helpful:
template<typename... ComponentTypes>
class IntersectionComponents {
public:
template<typename ComponentType>
ComponentType& get() {
return *getByType<ComponentType*>(components);
}
private:
std::tuple<ComponentTypes* ...> components;
template<typename ComponentType>
void set(ComponentType& component) {
getByType<ComponentType*>(components) = &component;
}
friend class ComponentsManager;
};
getByType function returns reference to object in a tuple with the specified type(in template parameter).
Here's the error list from compiler(completely incomprehensible for me ;/):
In file included from /usr/include/c++/5.2.0/functional:55:0,
from /usr/include/c++/5.2.0/memory:79,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:426,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/tuple: In instantiation of ‘struct std::tuple_element<1ul, std::tuple<BarComponent*> >’:
/usr/include/c++/5.2.0/tuple:755:12: required from ‘struct std::tuple_element<2ul, std::tuple<FooComponent*, BarComponent*> >’
/usr/include/c++/5.2.0/tuple:769:69: required by substitution of ‘template<long unsigned int __i, class _Tp> using __tuple_element_t = typename std::tuple_element::type [with long unsigned int __i = 2ul; _Tp = std::tuple<FooComponent*, BarComponent*>]’
/usr/include/c++/5.2.0/tuple:844:5: required by substitution of ‘template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >&& std::get(std::tuple<_Elements ...>&&) [with long unsigned int __i = 2ul; _Elements = {FooComponent*, BarComponent*}]’
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h:31:103: required from ‘T& getByType(std::tuple<_Elements ...>&) [with T = BarComponent**; TupleElems = {FooComponent*, BarComponent*}]’
/mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:27:34: required from ‘void IntersectionComponents<ComponentTypes>::set(ComponentType&) [with ComponentType = BarComponent*; ComponentTypes = {FooComponent, BarComponent}]’
/mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:148:13: required from ‘bool ComponentsManager::allComponentsExist(EntityID, IntersectComponents&) [with IntersectComponents = IntersectionComponents<FooComponent, BarComponent>; Head = BarComponent; Tail = {}; EntityID = unsigned int]’
/mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:118:83: required from ‘std::vector<IntersectionComponents<Head, Tail ...> > ComponentsManager::intersection() [with Head = FooComponent; Tail = {BarComponent}]’
/mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:75:64: required from here
/usr/include/c++/5.2.0/tuple:755:12: error: invalid use of incomplete type ‘struct std::tuple_element<0ul, std::tuple<> >’
struct tuple_element<__i, tuple<_Head, _Tail...> >
^
In file included from /usr/include/c++/5.2.0/algorithm:60:0,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:67,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/utility:85:11: note: declaration of ‘struct std::tuple_element<0ul, std::tuple<> >’
class tuple_element;
^
In file included from /mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:4:0,
from /mnt/data/dev/Active/ECS/include/../src/core/engine.h:4,
from /mnt/data/dev/Active/ECS/./include/ecs/ecs.h:3,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:2:
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h: In instantiation of ‘T& getByType(std::tuple<_Elements ...>&) [with T = BarComponent**; TupleElems = {FooComponent*, BarComponent*}]’:
/mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:27:34: required from ‘void IntersectionComponents<ComponentTypes>::set(ComponentType&) [with ComponentType = BarComponent*; ComponentTypes = {FooComponent, BarComponent}]’
/mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:148:13: required from ‘bool ComponentsManager::allComponentsExist(EntityID, IntersectComponents&) [with IntersectComponents = IntersectionComponents<FooComponent, BarComponent>; Head = BarComponent; Tail = {}; EntityID = unsigned int]’
/mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:118:83: required from ‘std::vector<IntersectionComponents<Head, Tail ...> > ComponentsManager::intersection() [with Head = FooComponent; Tail = {BarComponent}]’
/mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:75:64: required from here
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h:31:103: error: no matching function for call to ‘get(std::tuple<FooComponent*, BarComponent*>&)’
return std::get<detail::get_number_of_element_from_tuple_by_type_impl<T, 0, TupleElems...>::value>(t);
^
In file included from /usr/include/c++/5.2.0/algorithm:60:0,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:67,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/utility:147:5: note: candidate: template<long unsigned int _Int, class _Tp1, class _Tp2> constexpr typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type& std::get(std::pair<_Tp1, _Tp2>&)
get(std::pair<_Tp1, _Tp2>& __in) noexcept
^
/usr/include/c++/5.2.0/utility:147:5: note: template argument deduction/substitution failed:
In file included from /mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:4:0,
from /mnt/data/dev/Active/ECS/include/../src/core/engine.h:4,
from /mnt/data/dev/Active/ECS/./include/ecs/ecs.h:3,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:2:
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h:31:103: note: ‘std::tuple<FooComponent*, BarComponent*>’ is not derived from ‘std::pair<_Tp1, _Tp2>’
return std::get<detail::get_number_of_element_from_tuple_by_type_impl<T, 0, TupleElems...>::value>(t);
^
In file included from /usr/include/c++/5.2.0/algorithm:60:0,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:67,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/utility:152:5: note: candidate: template<long unsigned int _Int, class _Tp1, class _Tp2> constexpr typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type&& std::get(std::pair<_Tp1, _Tp2>&&)
get(std::pair<_Tp1, _Tp2>&& __in) noexcept
^
/usr/include/c++/5.2.0/utility:152:5: note: template argument deduction/substitution failed:
In file included from /mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:4:0,
from /mnt/data/dev/Active/ECS/include/../src/core/engine.h:4,
from /mnt/data/dev/Active/ECS/./include/ecs/ecs.h:3,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:2:
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h:31:103: note: ‘std::tuple<FooComponent*, BarComponent*>’ is not derived from ‘std::pair<_Tp1, _Tp2>’
return std::get<detail::get_number_of_element_from_tuple_by_type_impl<T, 0, TupleElems...>::value>(t);
^
In file included from /usr/include/c++/5.2.0/algorithm:60:0,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:67,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/utility:157:5: note: candidate: template<long unsigned int _Int, class _Tp1, class _Tp2> constexpr const typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type& std::get(const std::pair<_Tp1, _Tp2>&)
get(const std::pair<_Tp1, _Tp2>& __in) noexcept
^
/usr/include/c++/5.2.0/utility:157:5: note: template argument deduction/substitution failed:
In file included from /mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:4:0,
from /mnt/data/dev/Active/ECS/include/../src/core/engine.h:4,
from /mnt/data/dev/Active/ECS/./include/ecs/ecs.h:3,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:2:
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h:31:103: note: ‘std::tuple<FooComponent*, BarComponent*>’ is not derived from ‘const std::pair<_Tp1, _Tp2>’
return std::get<detail::get_number_of_element_from_tuple_by_type_impl<T, 0, TupleElems...>::value>(t);
^
In file included from /usr/include/c++/5.2.0/algorithm:60:0,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:67,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/utility:166:5: note: candidate: template<class _Tp, class _Up> constexpr _Tp& std::get(std::pair<_T1, _T2>&)
get(pair<_Tp, _Up>& __p) noexcept
^
/usr/include/c++/5.2.0/utility:166:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/utility:171:5: note: candidate: template<class _Tp, class _Up> constexpr const _Tp& std::get(const std::pair<_T1, _T2>&)
get(const pair<_Tp, _Up>& __p) noexcept
^
/usr/include/c++/5.2.0/utility:171:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/utility:176:5: note: candidate: template<class _Tp, class _Up> constexpr _Tp&& std::get(std::pair<_T1, _T2>&&)
get(pair<_Tp, _Up>&& __p) noexcept
^
/usr/include/c++/5.2.0/utility:176:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/utility:181:5: note: candidate: template<class _Tp, class _Up> constexpr _Tp& std::get(std::pair<_Up, _Tp>&)
get(pair<_Up, _Tp>& __p) noexcept
^
/usr/include/c++/5.2.0/utility:181:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/utility:186:5: note: candidate: template<class _Tp, class _Up> constexpr const _Tp& std::get(const std::pair<_Up, _Tp>&)
get(const pair<_Up, _Tp>& __p) noexcept
^
/usr/include/c++/5.2.0/utility:186:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/utility:191:5: note: candidate: template<class _Tp, class _Up> constexpr _Tp&& std::get(std::pair<_Up, _Tp>&&)
get(pair<_Up, _Tp>&& __p) noexcept
^
/usr/include/c++/5.2.0/utility:191:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5.2.0/tuple:39:0,
from /usr/include/c++/5.2.0/functional:55,
from /usr/include/c++/5.2.0/memory:79,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:426,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/array:280:5: note: candidate: template<long unsigned int _Int, class _Tp, long unsigned int _Nm> constexpr _Tp& std::get(std::array<_Tp, _Nm>&)
get(array<_Tp, _Nm>& __arr) noexcept
^
/usr/include/c++/5.2.0/array:280:5: note: template argument deduction/substitution failed:
In file included from /mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:4:0,
from /mnt/data/dev/Active/ECS/include/../src/core/engine.h:4,
from /mnt/data/dev/Active/ECS/./include/ecs/ecs.h:3,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:2:
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h:31:103: note: ‘std::tuple<FooComponent*, BarComponent*>’ is not derived from ‘std::array<_Tp, _Nm>’
return std::get<detail::get_number_of_element_from_tuple_by_type_impl<T, 0, TupleElems...>::value>(t);
^
In file included from /usr/include/c++/5.2.0/tuple:39:0,
from /usr/include/c++/5.2.0/functional:55,
from /usr/include/c++/5.2.0/memory:79,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:426,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/array:289:5: note: candidate: template<long unsigned int _Int, class _Tp, long unsigned int _Nm> constexpr _Tp&& std::get(std::array<_Tp, _Nm>&&)
get(array<_Tp, _Nm>&& __arr) noexcept
^
/usr/include/c++/5.2.0/array:289:5: note: template argument deduction/substitution failed:
In file included from /mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:4:0,
from /mnt/data/dev/Active/ECS/include/../src/core/engine.h:4,
from /mnt/data/dev/Active/ECS/./include/ecs/ecs.h:3,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:2:
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h:31:103: note: ‘std::tuple<FooComponent*, BarComponent*>’ is not derived from ‘std::array<_Tp, _Nm>’
return std::get<detail::get_number_of_element_from_tuple_by_type_impl<T, 0, TupleElems...>::value>(t);
^
In file included from /usr/include/c++/5.2.0/tuple:39:0,
from /usr/include/c++/5.2.0/functional:55,
from /usr/include/c++/5.2.0/memory:79,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:426,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/array:297:5: note: candidate: template<long unsigned int _Int, class _Tp, long unsigned int _Nm> constexpr const _Tp& std::get(const std::array<_Tp, _Nm>&)
get(const array<_Tp, _Nm>& __arr) noexcept
^
/usr/include/c++/5.2.0/array:297:5: note: template argument deduction/substitution failed:
In file included from /mnt/data/dev/Active/ECS/include/../src/core/componentsManager.h:4:0,
from /mnt/data/dev/Active/ECS/include/../src/core/engine.h:4,
from /mnt/data/dev/Active/ECS/./include/ecs/ecs.h:3,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:2:
/mnt/data/dev/Active/ECS/src/utils/getTupleElementByID.h:31:103: note: ‘std::tuple<FooComponent*, BarComponent*>’ is not derived from ‘const std::array<_Tp, _Nm>’
return std::get<detail::get_number_of_element_from_tuple_by_type_impl<T, 0, TupleElems...>::value>(t);
^
In file included from /usr/include/c++/5.2.0/functional:55:0,
from /usr/include/c++/5.2.0/memory:79,
from /mnt/data/dev/Active/ECS/src/3party/catch.hpp:426,
from /mnt/data/dev/Active/ECS/tests/core/componentsManagerTests.cpp:1:
/usr/include/c++/5.2.0/tuple:832:5: note: candidate: template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >& std::get(std::tuple<_Elements ...>&)
get(tuple<_Elements...>& __t) noexcept
^
/usr/include/c++/5.2.0/tuple:832:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/tuple:838:5: note: candidate: template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >& std::get(const std::tuple<_Elements ...>&)
get(const tuple<_Elements...>& __t) noexcept
^
/usr/include/c++/5.2.0/tuple:838:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/tuple:844:5: note: candidate: template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >&& std::get(std::tuple<_Elements ...>&&)
get(tuple<_Elements...>&& __t) noexcept
^
/usr/include/c++/5.2.0/tuple:844:5: note: substitution of deduced template arguments resulted in errors seen above
/usr/include/c++/5.2.0/tuple:867:5: note: candidate: template<class _Tp, class ... _Types> constexpr _Tp& std::get(std::tuple<_Elements ...>&)
get(tuple<_Types...>& __t) noexcept
^
/usr/include/c++/5.2.0/tuple:867:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/tuple:873:5: note: candidate: template<class _Tp, class ... _Types> constexpr _Tp&& std::get(std::tuple<_Elements ...>&&)
get(tuple<_Types...>&& __t) noexcept
^
/usr/include/c++/5.2.0/tuple:873:5: note: template argument deduction/substitution failed:
/usr/include/c++/5.2.0/tuple:879:5: note: candidate: template<class _Tp, class ... _Types> constexpr const _Tp& std::get(const std::tuple<_Elements ...>&)
get(const tuple<_Types...>& __t) noexcept
^
/usr/include/c++/5.2.0/tuple:879:5: note: template argument deduction/substitution failed:
CMakeFiles/tests.dir/build.make:77: polecenia dla obiektu 'CMakeFiles/tests.dir/tests/core/componentsManagerTests.cpp.o' nie powiodły się
Solved it. Well, it took 3 minutes or so after I've got 'genius' idea to just look at all error locations reported instead of trying to comprehend actual errors. And I've searched for error in the wrong place, due to IDE which underlined following line in red:
allComponentsExist<IntersectionComponents<Head, Tail...>, Tail...>(headComponent.entityID, currentEntityRequiredComponents));
Actual problem was this line:
components.set(currentComponent);
set() should get reference to object, but currentComponent was a pointer... changed it to:
components.set(*currentComponent);
And wall of errors is gone.
This line of errors made me realize this:
...componentsManager.h:27:34: required from ‘void IntersectionComponents<ComponentTypes>::set(ComponentType&) [with ComponentType = BarComponent*; ComponentTypes = {FooComponent, BarComponent}]’
Specifically "with ComponentType = BarComponent*" part :S
Anyway, sorry for bad question.
Problem
Having an adjacency list graph, I would like to traverse it with a DFS algorithm from a specific set of source nodes. The main problem is that the color map is passed by value.
I tried
To encapsulate the color map by reference into a structure :
class ref_color_map_wrapper
{
public:
typedef boost::default_color_type color_type;
typedef std::vector<color_type> color_map_type;
private:
color_map_type &color_map;
public:
ref_color_map_wrapper(color_map_type& color_map)
: color_map(color_map)
{}
color_type& operator[](size_t i)
{
return color_map[i];
}
const color_type& operator[](size_t i) const
{
return color_map[i];
}
};
namespace boost
{
template <>
struct property_traits<ref_color_map_wrapper>
{
typedef boost::read_write_property_map_tag category;
typedef boost::default_color_type value_type;
typedef boost::default_color_type& reference;
typedef size_t key_type;
};
void put(ref_color_map_wrapper& color_map, vertex_descriptor& v, boost::default_color_type color)
{
color_map[v] = color;
}
boost::default_color_type get(ref_color_map_wrapper& color_map, vertex_descriptor& v)
{
return color_map[v];
}
void put(ref_color_map_wrapper& color_map, const vertex_descriptor& v, boost::default_color_type color)
{
color_map[v] = color;
}
boost::default_color_type get(const ref_color_map_wrapper& color_map, const vertex_descriptor& v)
{
return color_map[v];
}
}
And finally the code of the DFS :
typedef std::vector<boost::default_color_type> color_map_type;
color_map_type color_map(boost::num_vertices(graph), boost::white_color);
ref_color_map_wrapper ref_color_map(color_map);
for(auto it = root_set.begin(); it != root_set.end(); ++it)
{
size_t i = boost::get(boost::vertex_index_t(), graph, *it);
if(color_map[i] == boost::white_color)
{
boost::depth_first_visit(graph, *it, boost::default_dfs_visitor(), ref_color_map);
}
}
Compilation error
/usr/local/include/boost/property_map/property_map.hpp: In instantiation of ‘void boost::ReadablePropertyMapConcept<PMap, Key>::constraints() [with PMap = gc::ref_color_map_wrapper; Key = long unsigned int]’:
/usr/local/include/boost/concept/detail/has_constraints.hpp:32:14: required by substitution of ‘template<class Model> boost::concepts::detail::yes boost::concepts::detail::has_constraints_(Model*, boost::concepts::detail::wrap_constraints<Model, (& Model:: constraints)>*) [with Model = boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int>]’
/usr/local/include/boost/concept/detail/has_constraints.hpp:42:5: required from ‘const bool boost::concepts::not_satisfied<boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >::value’
/usr/local/include/boost/concept/detail/has_constraints.hpp:45:31: required from ‘struct boost::concepts::not_satisfied<boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >’
/usr/local/include/boost/mpl/if.hpp:67:11: required from ‘struct boost::mpl::if_<boost::concepts::not_satisfied<boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >, boost::concepts::constraint<boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >, boost::concepts::requirement<boost::concepts::failed************ boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int>::************> >’
/usr/local/include/boost/concept/detail/general.hpp:50:8: required from ‘struct boost::concepts::requirement_<void (*)(boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int>)>’
/usr/local/include/boost/concept_check.hpp:45:1: [ skipping 4 instantiation contexts ]
/usr/local/include/boost/concept/detail/has_constraints.hpp:45:31: required from ‘struct boost::concepts::not_satisfied<boost::ReadWritePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >’
/usr/local/include/boost/mpl/if.hpp:67:11: required from ‘struct boost::mpl::if_<boost::concepts::not_satisfied<boost::ReadWritePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >, boost::concepts::constraint<boost::ReadWritePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >, boost::concepts::requirement<boost::concepts::failed************ boost::ReadWritePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int>::************> >’
/usr/local/include/boost/concept/detail/general.hpp:50:8: required from ‘struct boost::concepts::requirement_<void (*)(boost::ReadWritePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int>)>’
/usr/local/include/boost/graph/depth_first_search.hpp:88:1: required from ‘void boost::detail::depth_first_visit_impl(const IncidenceGraph&, typename boost::graph_traits<Graph>::vertex_descriptor, DFSVisitor&, ColorMap, TerminatorFunc) [with IncidenceGraph = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, boost::variant<const void*, std::pair<void*, void*> > >; DFSVisitor = boost::dfs_visitor<>; ColorMap = gc::ref_color_map_wrapper; TerminatorFunc = boost::detail::nontruth2; typename boost::graph_traits<Graph>::vertex_descriptor = long unsigned int]’
/usr/local/include/boost/graph/depth_first_search.hpp:314:5: required from ‘void boost::depth_first_visit(const IncidenceGraph&, typename boost::graph_traits<Graph>::vertex_descriptor, DFSVisitor, ColorMap) [with IncidenceGraph = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, boost::variant<const void*, std::pair<void*, void*> > >; DFSVisitor = boost::dfs_visitor<>; ColorMap = gc::ref_color_map_wrapper; typename boost::graph_traits<Graph>::vertex_descriptor = long unsigned int]’
../include/garbage_collector.hpp:169:87: required from here
/usr/local/include/boost/property_map/property_map.hpp:200:7: error: no matching function for call to ‘get(gc::ref_color_map_wrapper&, long unsigned int&)’
/usr/local/include/boost/property_map/property_map.hpp:200:7: note: candidates are:
In file included from /usr/local/include/boost/tuple/tuple.hpp:33:0,
from /usr/local/include/boost/unordered/detail/allocate.hpp:27,
from /usr/local/include/boost/unordered/detail/buckets.hpp:15,
from /usr/local/include/boost/unordered/detail/table.hpp:10,
from /usr/local/include/boost/unordered/detail/equivalent.hpp:14,
from /usr/local/include/boost/unordered/unordered_set.hpp:17,
from /usr/local/include/boost/unordered_set.hpp:16,
from /usr/local/include/boost/graph/adjacency_list.hpp:21,
from ../include/garbage_collector.hpp:6,
from main.cpp:3:
/usr/local/include/boost/tuple/detail/tuple_basic.hpp:225:1: note: template<int N, class HT, class TT> typename boost::tuples::access_traits<typename boost::tuples::element<N, boost::tuples::cons<HT, TT> >::type>::const_type boost::tuples::get(const boost::tuples::cons<HT, TT>&)
/usr/local/include/boost/tuple/detail/tuple_basic.hpp:225:1: note: template argument deduction/substitution failed:
In file included from /usr/local/include/boost/graph/adjacency_list.hpp:36:0,
from ../include/garbage_collector.hpp:6,
from main.cpp:3:
/usr/local/include/boost/property_map/property_map.hpp:200:7: note: ‘gc::ref_color_map_wrapper’ is not derived from ‘const boost::tuples::cons<HT, TT>’
In file included from /usr/local/include/boost/tuple/tuple.hpp:33:0,
from /usr/local/include/boost/unordered/detail/allocate.hpp:27,
from /usr/local/include/boost/unordered/detail/buckets.hpp:15,
from /usr/local/include/boost/unordered/detail/table.hpp:10,
from /usr/local/include/boost/unordered/detail/equivalent.hpp:14,
from /usr/local/include/boost/unordered/unordered_set.hpp:17,
from /usr/local/include/boost/unordered_set.hpp:16,
from /usr/local/include/boost/graph/adjacency_list.hpp:21,
from ../include/garbage_collector.hpp:6,
from main.cpp:3:
/usr/local/include/boost/tuple/detail/tuple_basic.hpp:211:1: note: template<int N, class HT, class TT> typename boost::tuples::access_traits<typename boost::tuples::element<N, boost::tuples::cons<HT, TT> >::type>::non_const_type boost::tuples::get(boost::tuples::cons<HT, TT>&)
/usr/local/include/boost/tuple/detail/tuple_basic.hpp:211:1: note: template argument deduction/substitution failed:
In file included from /usr/local/include/boost/graph/adjacency_list.hpp:36:0,
from ../include/garbage_collector.hpp:6,
from main.cpp:3:
/usr/local/include/boost/property_map/property_map.hpp:200:7: note: ‘gc::ref_color_map_wrapper’ is not derived from ‘boost::tuples::cons<HT, TT>’
/usr/local/include/boost/property_map/property_map.hpp:179:19: note: template<class T> const T& get(const T*, std::ptrdiff_t)
/usr/local/include/boost/property_map/property_map.hpp:179:19: note: template argument deduction/substitution failed:
/usr/local/include/boost/property_map/property_map.hpp:200:7: note: mismatched types ‘const T*’ and ‘gc::ref_color_map_wrapper’
Graph definition
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, vertex_info_type> graph_type;
What's working
boost::get(ref_color_map, *it);
boost::put(ref_color_map, *it, boost::white_color);
works without any compilation error…
You need to put your get() and put() functions in the namespace where ref_color_map resides, since they are found via ADL. See here.