Storing a unique_ptr with custom deleter in a map - c++

Why doesn't this work?
#include <map>
#include <memory>
void deleter(int* i) {
delete i;
}
std::map<int, std::unique_ptr<int, decltype(&deleter)>> m;
void foo(int* i) {
m[0] = std::unique_ptr<int, decltype(&deleter)>(i, &deleter);
}
Check out the incomprehensible compile error https://godbolt.org/z/Uhp9NO.
In file included from <source>:1:
In file included from /opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/map:61:
In file included from /opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/stl_map.h:63:
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/tuple:1668:9: error: no matching constructor for initialization of 'std::unique_ptr<int, void (*)(int *)>'
second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/tuple:1655:9: note: in instantiation of function template specialization 'std::pair<const int, std::unique_ptr<int, void (*)(int *)> >::pair<int &&, 0>' requested here
: pair(__first, __second,
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/ext/new_allocator.h:136:23: note: in instantiation of function template specialization 'std::pair<const int, std::unique_ptr<int, void (*)(int *)> >::pair<int &&>' requested here
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/alloc_traits.h:475:8: note: in instantiation of function template specialization '__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > > >::construct<std::pair<const int, std::unique_ptr<int, void (*)(int *)> >, const std::piecewise_construct_t &, std::tuple<int &&>, std::tuple<> >' requested here
{ __a.construct(__p, std::forward<_Args>(__args)...); }
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/stl_tree.h:637:23: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<std::_Rb_tree_node<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > > > >::construct<std::pair<const int, std::unique_ptr<int, void (*)(int *)> >, const std::piecewise_construct_t &, std::tuple<int &&>, std::tuple<> >' requested here
_Alloc_traits::construct(_M_get_Node_allocator(),
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/stl_tree.h:654:4: note: in instantiation of function template specialization 'std::_Rb_tree<int, std::pair<const int, std::unique_ptr<int, void (*)(int *)> >, std::_Select1st<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > >, std::less<int>, std::allocator<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > > >::_M_construct_node<const std::piecewise_construct_t &, std::tuple<int &&>, std::tuple<> >' requested here
_M_construct_node(__tmp, std::forward<_Args>(__args)...);
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/stl_tree.h:2414:19: note: in instantiation of function template specialization 'std::_Rb_tree<int, std::pair<const int, std::unique_ptr<int, void (*)(int *)> >, std::_Select1st<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > >, std::less<int>, std::allocator<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > > >::_M_create_node<const std::piecewise_construct_t &, std::tuple<int &&>, std::tuple<> >' requested here
_Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/stl_map.h:518:15: note: in instantiation of function template specialization 'std::_Rb_tree<int, std::pair<const int, std::unique_ptr<int, void (*)(int *)> >, std::_Select1st<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > >, std::less<int>, std::allocator<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > > >::_M_emplace_hint_unique<const std::piecewise_construct_t &, std::tuple<int &&>, std::tuple<> >' requested here
__i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct,
^
<source>:11:6: note: in instantiation of member function 'std::map<int, std::unique_ptr<int, void (*)(int *)>, std::less<int>, std::allocator<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > > >::operator[]' requested here
m[0] = std::unique_ptr<int, decltype(&deleter)>(i, &deleter);
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:191:12: note: candidate template ignored: substitution failure [with _Up = void (*)(int *)]: no type named 'type' in 'std::enable_if<false, void>'
constexpr unique_ptr() noexcept
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:204:2: note: candidate constructor template not viable: requires single argument '__p', but no arguments were provided
unique_ptr(pointer __p) noexcept
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:236:12: note: candidate constructor template not viable: requires 1 argument, but 0 were provided
constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:255:2: note: candidate constructor template not viable: requires single argument '__u', but no arguments were provided
unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:265:2: note: candidate constructor template not viable: requires single argument '__u', but no arguments were provided
unique_ptr(auto_ptr<_Up>&& __u) noexcept;
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:241:7: note: candidate constructor not viable: requires single argument '__u', but no arguments were provided
unique_ptr(unique_ptr&& __u) noexcept
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:394:7: note: candidate constructor not viable: requires 1 argument, but 0 were provided
unique_ptr(const unique_ptr&) = delete;
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:215:7: note: candidate constructor not viable: requires 2 arguments, but 0 were provided
unique_ptr(pointer __p,
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:227:7: note: candidate constructor not viable: requires 2 arguments, but 0 were provided
unique_ptr(pointer __p,
^
1 error generated.
Compiler returned: 1

The problem is that m[0] calls the default constructor of std::unique_ptr<int, decltype(&deleter)>, which is not available because it requires the deleter pointer.
Fix:
struct Deleter {
void operator()(int* i) { delete i; }
};
std::map<int, std::unique_ptr<int, Deleter>> m;
void foo(int* i) {
m[0] = std::unique_ptr<int, Deleter>(i);
}
This is also more efficient than std::unique_ptr<int, decltype(&deleter)> because it does not have to store the very same pointer to deleter in each instance of std::unique_ptr. I.e. sizeof(std::unique_ptr<int, Deleter>) < sizeof(std::unique_ptr<int, decltype(&deleter)>).

std::unique_ptr, when using a custom deleter function like you have, is not default constructable. std::map::operator[] requires that the value type of the map be default constructable. That means as is you cannot use operator [] with this type of map.
In order for std::unique_ptr to be default constructable you need a deleter that satisfies where std::is_default_constructible<Deleter>::value is true and Deleter is not a pointer type.

Related

How to correctly use a map of sets with custom comparator in C++?

I want to use a map of sets, with the set having a custom comparator. Here is my code:
using item = pair<string, int>;
auto comp = [](item const& p1, item const& p2) {return p1.second < p2.second;};
class TimeMap {
unordered_map<string, decltype(set<item, decltype(comp)>(comp))> data;
public:
TimeMap() {
data.clear();
}
void set(string key, string value, int timestamp) {
data[key].insert({value, timestamp});
}
};
This yields the following compile error:
In file included from prog_joined.cpp:1:
In file included from ./precompiled/headers.h:50:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/map:60:
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_tree.h:149:9: error: no matching constructor for initialization of '(lambda at prog_joined.cpp:7:13)'
: _M_key_compare()
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_tree.h:680:4: note: in instantiation of member function 'std::_Rb_tree_key_compare<(lambda at prog_joined.cpp:7:13)>::_Rb_tree_key_compare' requested here
_Rb_tree_impl()
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_tree.h:939:7: note: in instantiation of member function 'std::_Rb_tree<std::pair<std::__cxx11::basic_string<char>, int>, std::pair<std::__cxx11::basic_string<char>, int>, std::_Identity<std::pair<std::__cxx11::basic_string<char>, int>>, (lambda at prog_joined.cpp:7:13), std::allocator<std::pair<std::__cxx11::basic_string<char>, int>>>::_Rb_tree_impl<(lambda at prog_joined.cpp:7:13), false>::_Rb_tree_impl' requested here
_Rb_tree() = default;
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/tuple:1661:9: note: in instantiation of function template specialization 'std::pair<const std::__cxx11::basic_string<char>, std::set<std::pair<std::__cxx11::basic_string<char>, int>, (lambda at prog_joined.cpp:7:13), std::allocator<std::pair<std::__cxx11::basic_string<char>, int>>>>::pair<const std::__cxx11::basic_string<char> &, 0>' requested here
: pair(__first, __second,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/ext/new_allocator.h:147:23: note: in instantiation of function template specialization 'std::pair<const std::__cxx11::basic_string<char>, std::set<std::pair<std::__cxx11::basic_string<char>, int>, (lambda at prog_joined.cpp:7:13), std::allocator<std::pair<std::__cxx11::basic_string<char>, int>>>>::pair<const std::__cxx11::basic_string<char> &>' requested here
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/alloc_traits.h:484:8: note: in instantiation of function template specialization '__gnu_cxx::new_allocator<std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, std::set<std::pair<std::__cxx11::basic_string<char>, int>, (lambda at prog_joined.cpp:7:13), std::allocator<std::pair<std::__cxx11::basic_string<char>, int>>>>, true>>::construct<std::pair<const std::__cxx11::basic_string<char>, std::set<std::pair<std::__cxx11::basic_string<char>, int>, (lambda at prog_joined.cpp:7:13), std::allocator<std::pair<std::__cxx11::basic_string<char>, int>>>>, const std::piecewise_construct_t &, std::tuple<const std::__cxx11::basic_string<char> &>, std::tuple<>>' requested here
{ __a.construct(__p, std::forward<_Args>(__args)...); }
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/hashtable_policy.h:2086:27: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, std::set<std::pair<std::__cxx11::basic_string<char>, int>, (lambda at prog_joined.cpp:7:13), std::allocator<std::pair<std::__cxx11::basic_string<char>, int>>>>, true>>>::construct<std::pair<const std::__cxx11::basic_string<char>, std::set<std::pair<std::__cxx11::basic_string<char>, int>, (lambda at prog_joined.cpp:7:13), std::allocator<std::pair<std::__cxx11::basic_string<char>, int>>>>, const std::piecewise_construct_t &, std::tuple<const std::__cxx11::basic_string<char> &>, std::tuple<>>' requested here
__node_alloc_traits::construct(_M_node_allocator(),
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/hashtable_policy.h:701:15: note: in instantiation of function template specialization 'std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, std::set<std::pair<std::__cxx11::basic_string<char>, int>, (lambda at prog_joined.cpp:7:13), std::allocator<std::pair<std::__cxx11::basic_string<char>, int>>>>, true>>>::_M_allocate_node<const std::piecewise_construct_t &, std::tuple<const std::__cxx11::basic_string<char> &>, std::tuple<>>' requested here
__p = __h->_M_allocate_node(std::piecewise_construct,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/unordered_map.h:985:16: note: in instantiation of member function 'std::__detail::_Map_base<std::__cxx11::basic_string<char>, std::pair<const std::__cxx11::basic_string<char>, std::set<std::pair<std::__cxx11::basic_string<char>, int>, (lambda at prog_joined.cpp:7:13), std::allocator<std::pair<std::__cxx11::basic_string<char>, int>>>>, std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::set<std::pair<std::__cxx11::basic_string<char>, int>, (lambda at prog_joined.cpp:7:13), std::allocator<std::pair<std::__cxx11::basic_string<char>, int>>>>>, std::__detail::_Select1st, std::equal_to<std::__cxx11::basic_string<char>>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true>, true>::operator[]' requested here
{ return _M_h[__k]; }
^
Line 13: Char 13: note: in instantiation of member function 'std::unordered_map<std::__cxx11::basic_string<char>, std::set<std::pair<std::__cxx11::basic_string<char>, int>, (lambda at solution.cpp:7:13), std::allocator<std::pair<std::__cxx11::basic_string<char>, int>>>, std::hash<std::string>, std::equal_to<std::__cxx11::basic_string<char>>, std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::set<std::pair<std::__cxx11::basic_string<char>, int>, (lambda at solution.cpp:7:13), std::allocator<std::pair<std::__cxx11::basic_string<char>, int>>>>>>::operator[]' requested here
data[key].insert({value, timestamp});
^
Line 2: Char 13: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
auto comp = [](item const& p1, item const& p2) {return p1.second < p2.second;};
^
Line 2: Char 13: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided
1 error generated.
In this line : data[key].insert({value, timestamp});, a new set (with my custom comparator) is tentatively instantiated as a value of the map, but that fails. What causes this failure and how to fix it?
The value of unordered_map is set. set requires a comparator object which should be default constructible. Unfortunately, lambdas are default constructible since C++20, so compile your code at least with this standard, or use functor object like:
struct Comp {
bool operator()(const item& lhs, const item& rhs) const {
return lhs.second < rhs.second;
}
};
class TimeMap
{
unordered_map<string, set<item, Comp> > data;
instead of lambda.

Stop compiler to suggest candidates for overloaded template, expand PRETTY_FUNCTION in static_assert

My code use templates heavily. I have a number of overloaded functions
auto operator>>(byte_vector_view&bvv, Type&&) ->byte_vector_view&;
This works fine until unknown by this function family type is passed to function:
bvv >> my_custom;
User should simply see one error message:
There no implementation for operator>>(byte_vector_view&bvv, my_custom_type&)
instead we see a very long manuscript:
how to put this under spoiler?
itm.cpp:184:18: error: invalid operands to binary expression ('byte_vector_view' and 'named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >')
((bvv>>elements),...);
~~~^ ~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/type_traits:4345:23: note: in instantiation of function template specialization 'operator>>(byte_vector_view &, std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > &)::(anonymous class)::operator()<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' requested here
_LIBCPP_INVOKE_RETURN(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...))
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/__config:508:15: note: expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_NAMESPACE
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1375:5: note: while substituting deduced template arguments into function template '__invoke_constexpr' [with _Fp = (lambda at itm.cpp:183:9), _Args = <named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > &>]
_VSTD::__invoke_constexpr(
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/__config:508:15: note: expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_NAMESPACE
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1372:26: note: in instantiation of exception specification for '__apply_tuple_impl<(lambda at itm.cpp:183:9), std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > &, 0>' requested here
constexpr decltype(auto) __apply_tuple_impl(_Fn && __f, _Tuple && __t,
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1384:12: note: in instantiation of function template specialization 'std::__1::__apply_tuple_impl<(lambda at itm.cpp:183:9), std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > &, 0>' requested here
_VSTD::__apply_tuple_impl(
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1382:26: note: in instantiation of exception specification for 'apply<(lambda at itm.cpp:183:9), std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > &>' requested here
constexpr decltype(auto) apply(_Fn && __f, _Tuple && __t)
^
itm.cpp:182:5: note: in instantiation of function template specialization 'std::__1::apply<(lambda at itm.cpp:183:9), std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > &>' requested here
apply(
^
itm.cpp:461:20: note: in instantiation of function template specialization 'operator>><std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > >' requested here
bvv>>tvs;
^
itm.cpp:427:12: note: in instantiation of member function 'make_converter(bool, std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> > &&, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > &&)::Ret::from_byte_vector_view' requested here
struct Ret:converter_virtual{
^
itm.cpp:537:12: note: in instantiation of function template specialization 'make_converter<std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> >, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' requested here
return make_converter(false,forward<Prefix>(prefix),forward<Ts>(values)...);
^
itm.cpp:931:18: note: in instantiation of function template specialization 'make_converter<std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> >, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' requested here
/*response*/make_converter(
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/type_traits:4840:3: note: candidate function template not viable: no known conversion from 'byte_vector_view' to 'std::byte' for 1st argument
operator>> (byte __lhs, _Integer __shift) noexcept
^
itm.cpp:147:19: note: candidate function not viable: no known conversion from 'named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >' to 'uint8_t &' (aka 'unsigned char &') for 2nd argument
byte_vector_view& operator>>(byte_vector_view&bvv, uint8_t&ui8){
^
itm.cpp:151:19: note: candidate function not viable: no known conversion from 'named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >' to 'std::__1::string &' (aka 'basic_string<char, char_traits<char>, allocator<char> > &') for 2nd argument
byte_vector_view& operator>>(byte_vector_view&bvv, string&str){
^
itm.cpp:156:19: note: candidate function not viable: no known conversion from 'named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >' to 'named_value<std::__1::string> &' (aka 'named_value<basic_string<char, char_traits<char>, allocator<char> > > &') for 2nd argument
byte_vector_view& operator>>(byte_vector_view&bvv, named_value<string>&av){
^
itm.cpp:161:6: note: candidate template ignored: requirement 'is_integral_v<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > || is_same_v<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >, ringnet::proto::opcode> || is_same_v<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >, command_address>' was not satisfied [with T = std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >]
auto operator>>(byte_vector_view&bvv, named_value<T>&av) ->enable_if_t<is_integral_v<T> || is_same_v<T,opcode> || is_same_v<T,command_address>, byte_vector_view&>{
^
itm.cpp:181:6: note: candidate template ignored: requirement 'is_tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' was not satisfied [with TupleT = named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >]
auto operator>>(byte_vector_view&bvv, TupleT&tu) ->enable_if_t<is_tuple<TupleT>,byte_vector_view&> {
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:521:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:570:1: note: candidate template ignored: could not match 'basic_istream<char, type-parameter-0-0>' against 'byte_vector_view'
operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:578:1: note: candidate template ignored: could not match 'basic_istream<char, type-parameter-0-0>' against 'byte_vector_view'
operator>>(basic_istream<char, _Traits>& __is, signed char* __s)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:585:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:613:1: note: candidate template ignored: could not match 'basic_istream<char, type-parameter-0-0>' against 'byte_vector_view'
operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:621:1: note: candidate template ignored: could not match 'basic_istream<char, type-parameter-0-0>' against 'byte_vector_view'
operator>>(basic_istream<char, _Traits>& __is, signed char& __c)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:1219:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp&& __x)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:1287:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>& __is,
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:1419:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iomanip:302:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x)
It's hard to read and understand where is the problem exactly and therefore hard to fix the problem
Need more clear explanation
I found one straightforward way to suppress excess output:
template<typename UnknownType>
auto operator>>(byte_vector_view&bvv, UnknownType&&) ->byte_vector_view& {
static_assert(false);
return bvv;
}
With it compiler gives a quiet short error message, too short:
itm.cpp:192:5: error: static_assert failed
static_assert(false);
^ ~~~~~
1 error generated.
I'd like to see in which function the error appeared and a trace how compiler come there:
template<typename UnknownType>
auto operator>>(byte_vector_view&bvv, UnknownType&&) ->byte_vector_view& {
static_assert(false&&__PRETTY_FUNCTION__);
return bvv;
}
itm.cpp:192:5: error: static_assert failed
static_assert(false && __PRETTY_FUNCTION__);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
itm.cpp:184:18: note: in instantiation of function template specialization 'operator>><named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > &>' requested here
((bvv>>elements),...);
^
itm.cpp:461:20: note: in instantiation of function template specialization 'operator>><std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > >' requested here
bvv>>tvs;
^
itm.cpp:427:12: note: in instantiation of member function 'make_converter(bool, std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> > &&, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > &&)::Ret::from_byte_vector_view' requested here
struct Ret:converter_virtual{
^
itm.cpp:537:12: note: in instantiation of function template specialization 'make_converter<std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> >, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' requested here
return make_converter(false,forward<Prefix>(prefix),forward<Ts>(values)...);
^
itm.cpp:931:18: note: in instantiation of function template specialization 'make_converter<std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> >, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' requested here
/*response*/make_converter(
^
1 error generated.
And this is almost looks like I want but Clang++ does not expand __PRETTY_FUNCTION__ in static_assert.
Is there a way to put expanded UnknownType to static_assert's message?
I also tried
static_assert(false,__PRETTY_FUNCTION__);
but it gives
error: expected string literal for diagnostic message in static_assert
#include <type_traits>
template <typename UnknownType>
struct Type_Not_Supported : std::false_type {};
template <typename UnknownType>
auto operator>>(byte_vector_view& bvv, UnknownType&&) -> byte_vector_view& {
static_assert(Type_Not_Supported<UnknownType>{});
return bvv;
}
In Clang, this yields:
prog.cc:10:5: error: static_assert failed due to requirement 'Type_Not_Supported<int>{}'
static_assert(Type_Not_Supported<UnknownType>{});
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:17:9: note: in instantiation of function template specialization 'operator>><int>' requested here
bvv >> 1;
^
1 error generated.
In GCC:
prog.cc: In instantiation of 'byte_vector_view& operator>>(byte_vector_view&, UnknownType&&) [with UnknownType = int]':
prog.cc:17:12: required from here
prog.cc:10:19: error: static assertion failed
10 | static_assert(Type_Not_Supported<UnknownType>{});
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DEMO

Compilation error when passing functional object and its arguments to std::thread constructor

I'm trying to compile the following code, but on the line threads[i] = std::thread(find_elements[i], block_start, block_end, match); the std::thread object cannot be constructed with given arguments.
#include <iostream>
#include <thread>
#include <vector>
#include <future>
class join_threads
{
public:
explicit join_threads(std::vector<std::thread>& threads)
: threads_(threads) {}
~join_threads()
{
for (size_t i = 0; i < threads_.size(); ++i)
{
if(threads_[i].joinable())
{
threads_[i].join();
}
}
}
private:
std::vector<std::thread>& threads_;
};
template <typename Iterator, typename MatchType>
Iterator parallel_find(Iterator first, Iterator last, MatchType match)
{
class find_element
{
public:
void operator()(Iterator begin, Iterator end, MatchType match)
{
try
{
while (begin != end)
{
if (*begin == match)
{
result_.set_value(begin);
is_found_ = true;
return;
}
++begin;
}
result_.set_value(end);
}
catch(...)
{
result_.set_exception(std::current_exception());
}
}
Iterator get() { return result_.get_future().get(); }
bool is_found() const { return is_found_; }
private:
std::promise<Iterator> result_;
bool is_found_ = false;
};
const unsigned length = std::distance(first, last);
if (0 == length) return last;
const unsigned min_per_thread = 25;
const unsigned max_threads = (length + min_per_thread - 1) / min_per_thread;
const unsigned hardware_threads = std::thread::hardware_concurrency();
const unsigned num_threads = std::min(
hardware_threads != 0 ? hardware_threads : 2, max_threads);
const unsigned block_size = length / num_threads;
std::vector<std::thread> threads(num_threads - 1);
std::vector<find_element> find_elements(num_threads);
{
join_threads joiner(threads);
Iterator block_start = first;
for (unsigned i = 0; i < num_threads - 1; ++i)
{
Iterator block_end = block_start;
std::advance(block_end, block_size);
threads[i] = std::thread(find_elements[i], block_start, block_end, match);
block_start = block_end;
}
find_elements[num_threads - 1](block_start, last, match);
}
for (unsigned i = 0; i < num_threads - 1; ++i)
{
if (find_elements[i].is_found())
{
return find_elements[i].get();
}
}
return find_elements[num_threads - 1].get();
}
int main()
{
std::vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8 };
parallel_find(v.begin(), v.end(), 9);
return 0;
}
The exact error produced by clang++ version 3.9.1 is the following:
In file included from ../../../sources/test/main.cpp:2:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/thread:39:
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/functional:1379:11: error: no matching constructor for initialization of 'std::tuple<find_element, __normal_iterator<int *, vector<int, allocator<int> > >, __normal_iterator<int *, vector<int, allocator<int> > >, int>'
: _M_bound(std::forward<_Tp>(__f), std::forward<_Up>(__args)...)
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/functional:1426:14: note: in instantiation of function template specialization 'std::_Bind_simple<find_element (__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, int)>::_Bind_simple<const find_element &, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &, int &>' requested here
return __result_type(
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/thread:137:13: note: in instantiation of function template specialization 'std::__bind_simple<find_element &, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &, int &>' requested here
std::__bind_simple(std::forward<_Callable>(__f),
^
../../../sources/test/main.cpp:84:20: note: in instantiation of function template specialization 'std::thread::thread<find_element &, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &, int &>' requested here
threads[i] = std::thread(find_elements[i], block_start, block_end, match);
^
../../../sources/test/main.cpp:105:3: note: in instantiation of function template specialization 'parallel_find<__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, int>' requested here
parallel_find(v.begin(), v.end(), 9);
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:600:18: note: candidate template ignored: disabled by 'enable_if' [with _Dummy = void]
_TCC<_Dummy>::template
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:611:18: note: candidate template ignored: disabled by 'enable_if' [with _Dummy = void]
_TCC<_Dummy>::template
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:628:5: note: candidate template ignored: disabled by 'enable_if' [with _UElements = <const find_element &, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &, int &>]
_TC<sizeof...(_UElements) == 1, _Elements...>::template
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:641:5: note: candidate template ignored: disabled by 'enable_if' [with _UElements = <const find_element &, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &, int &>]
_TC<sizeof...(_UElements) == 1, _Elements...>::template
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:737:19: note: candidate template ignored: disabled by 'enable_if' [with _Alloc = __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, _UElements = <__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &, int &>]
enable_if<_TMC<_UElements...>::template
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:748:19: note: candidate template ignored: disabled by 'enable_if' [with _Alloc = __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, _UElements = <__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &, int &>]
enable_if<_TMC<_UElements...>::template
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:579:17: note: candidate constructor template not viable: requires 0 arguments, but 4 were provided
constexpr tuple()
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:589:26: note: candidate constructor template not viable: requires 0 arguments, but 4 were provided
explicit constexpr tuple()
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:670:19: note: candidate constructor template not viable: requires single argument '__in', but 4 arguments were provided
constexpr tuple(const tuple<_UElements...>& __in)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:682:28: note: candidate constructor template not viable: requires single argument '__in', but 4 arguments were provided
explicit constexpr tuple(const tuple<_UElements...>& __in)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:694:19: note: candidate constructor template not viable: requires single argument '__in', but 4 arguments were provided
constexpr tuple(tuple<_UElements...>&& __in)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:705:28: note: candidate constructor template not viable: requires single argument '__in', but 4 arguments were provided
explicit constexpr tuple(tuple<_UElements...>&& __in)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:721:2: note: candidate constructor template not viable: requires 6 arguments, but 4 were provided
tuple(allocator_arg_t __tag, const _Alloc& __a,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:732:11: note: candidate constructor template not viable: requires 6 arguments, but 4 were provided
explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:711:2: note: candidate constructor template not viable: requires 2 arguments, but 4 were provided
tuple(allocator_arg_t __tag, const _Alloc& __a)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:759:2: note: candidate constructor template not viable: requires 3 arguments, but 4 were provided
tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:763:2: note: candidate constructor template not viable: requires 3 arguments, but 4 were provided
tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:772:2: note: candidate constructor template not viable: requires 3 arguments, but 4 were provided
tuple(allocator_arg_t __tag, const _Alloc& __a,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:784:11: note: candidate constructor template not viable: requires 3 arguments, but 4 were provided
explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:796:2: note: candidate constructor template not viable: requires 3 arguments, but 4 were provided
tuple(allocator_arg_t __tag, const _Alloc& __a,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:808:11: note: candidate constructor template not viable: requires 3 arguments, but 4 were provided
explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:654:17: note: candidate constructor not viable: requires 1 argument, but 4 were provided
constexpr tuple(tuple&&) = default;
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/tuple:652:17: note: candidate constructor not viable: requires 1 argument, but 4 were provided
constexpr tuple(const tuple&) = default;
^
1 error generated.
make: *** [main.o] Error 1

Why it is not possible to use an initializer_list to initialize a vector of unique_ptr's? [duplicate]

This question already has answers here:
initializer_list and move semantics
(9 answers)
Closed 8 years ago.
I'm wondering why initializer_list doesn't work with unique_ptr:
std::vector<std::unique_ptr<int>> vptr = {std::make_unique<int>(1), std::make_unique<int>(2)};
do not compile.
However:
std::vector<std::unique_ptr<int>> vptr(2);
vptr[0] =std::make_unique<int>(1);
vptr[1] =std::make_unique<int>(2);
compile.
and
std::vector<int*>vint={new int{1},new int{2}};
or
std::vector<std::shared_ptr<int>> vptr= {std::make_shared<int>(1), std::make_shared<int>(2)};
compile.
The unique_ptr variant is giving this error message in clang:
In file included from main.cpp:2:
In file included from /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/vector:62:
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_construct.h:75:38: error: call to deleted constructor of 'std::unique_ptr<int, std::default_delete<int> >'
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_uninitialized.h:75:8: note: in instantiation of function template specialization 'std::_Construct<std::unique_ptr<int, std::default_delete<int> >, const std::unique_ptr<int, std::default_delete<int> > &>' requested here
std::_Construct(std::__addressof(*__cur), *__first);
^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_uninitialized.h:125:2: note: in instantiation of function template specialization 'std::__uninitialized_copy<false>::__uninit_copy<const std::unique_ptr<int, std::default_delete<int> > *, std::unique_ptr<int, std::default_delete<int> > *>' requested here
__uninit_copy(__first, __last, __result);
^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_uninitialized.h:278:19: note: in instantiation of function template specialization 'std::uninitialized_copy<const std::unique_ptr<int, std::default_delete<int> > *, std::unique_ptr<int, std::default_delete<int> > *>' requested here
{ return std::uninitialized_copy(__first, __last, __result); }
^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_vector.h:1284:11: note: in instantiation of function template specialization 'std::__uninitialized_copy_a<const std::unique_ptr<int, std::default_delete<int> > *, std::unique_ptr<int, std::default_delete<int> > *, std::unique_ptr<int, std::default_delete<int> > >' requested here
std::__uninitialized_copy_a(__first, __last,
^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_vector.h:377:2: note: in instantiation of function template specialization 'std::vector<std::unique_ptr<int, std::default_delete<int> >, std::allocator<std::unique_ptr<int, std::default_delete<int> > > >::_M_range_initialize<const std::unique_ptr<int, std::default_delete<int> > *>' requested here
_M_range_initialize(__l.begin(), __l.end(),
^
main.cpp:10:42: note: in instantiation of member function 'std::vector<std::unique_ptr<int, std::default_delete<int> >, std::allocator<std::unique_ptr<int, std::default_delete<int> > > >::vector' requested here
std::vector<std::unique_ptr<int>> vptr = {std::make_unique<int>(1), std::make_unique<int>(2)};
^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/unique_ptr.h:356:7: note: 'unique_ptr' has been explicitly marked deleted here
unique_ptr(const unique_ptr&) = delete;
^
1 error generated.
The constructor for vector<T> taking an initializer_list<T> copies the elements from the initializer list into the vector. This does not work when you're dealing with an uncopyable type: the design of unique_ptr<T> prevents copies from being made. It does allow moving, which is why assignment works: make_unique returns an rvalue, for which the compiler knows it's safe to move.
You'd need some sort of constructor taking an initializer_list<T &&>, but C++ doesn't have such a constructor (nor indeed, as dyp pointed out, does the type initializer_list<T &&> even function).

Clang bug with std::function, std::bind and std::ref?

It seems the following code doesn't compile under clang (llvm version 5.0):
#include <functional>
int main()
{
int i;
std::function<void(int&)> f;
std::function<void()> f2 = std::bind(f, std::ref(i));
}
If f is declared as follows (i.e., not a std::function), then it compiles fine:
void f(int& n1);
Am I doing something wrong or is this really a compiler bug?
This is the compiler error I am getting:
In file included from test.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/functional:465:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:599:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/tuple:234:73: error: no matching constructor for initialization of 'std::__1::reference_wrapper<int>'
_NOEXCEPT_(is_nothrow_default_constructible<_Hp>::value) : value()
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/tuple:447:23: note: in instantiation of member function 'std::__1::__tuple_leaf<0, std::__1::reference_wrapper<int>, false>::__tuple_leaf' requested here
_LIBCPP_CONSTEXPR __tuple_impl()
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/tuple:550:23: note: in instantiation of member function 'std::__1::__tuple_impl<std::__1::__tuple_indices<0>, std::__1::reference_wrapper<int> >::__tuple_impl' requested
here
_LIBCPP_CONSTEXPR tuple()
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/functional:1744:11: note: in instantiation of member function 'std::__1::tuple<std::__1::reference_wrapper<int> >::tuple' requested here
__bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:2243:15: note: in instantiation of function template specialization 'std::__1::__bind<std::__1::function<void (int &)> &, std::__1::reference_wrapper<int>
>::__bind<std::__1::__bind<std::__1::function<void (int &)> &, std::__1::reference_wrapper<int> >, , void>' requested here
__first_(_VSTD::forward<_Args1>(get<_I1>(__first_args))...)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:2421:15: note: in instantiation of function template specialization 'std::__1::__libcpp_compressed_pair_imp<std::__1::__bind<std::__1::function<void (int &)> &,
std::__1::reference_wrapper<int> >, std::__1::allocator<std::__1::__bind<std::__1::function<void (int &)> &, std::__1::reference_wrapper<int> > >, 2>::__libcpp_compressed_pair_imp<std::__1::__bind<std::__1::function<void (int &)> &,
std::__1::reference_wrapper<int> > &&, , 0, >' requested here
: base(__pc, _VSTD::move(__first_args), _VSTD::move(__second_args),
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/functional:992:11: note: in instantiation of function template specialization 'std::__1::__compressed_pair<std::__1::__bind<std::__1::function<void (int &)> &,
std::__1::reference_wrapper<int> >, std::__1::allocator<std::__1::__bind<std::__1::function<void (int &)> &, std::__1::reference_wrapper<int> > > >::__compressed_pair<std::__1::__bind<std::__1::function<void (int &)> &, std::__1::reference_wrapper<int> > &&, >'
requested here
: __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/functional:1277:26: note: in instantiation of member function 'std::__1::__function::__func<std::__1::__bind<std::__1::function<void (int &)> &,
std::__1::reference_wrapper<int> >, std::__1::allocator<std::__1::__bind<std::__1::function<void (int &)> &, std::__1::reference_wrapper<int> > >, void ()>::__func' requested here
::new (__f_) _FF(_VSTD::move(__f));
^
test.cpp:7:29: note: in instantiation of function template specialization 'std::__1::function<void ()>::function<std::__1::__bind<std::__1::function<void (int &)> &, std::__1::reference_wrapper<int> > >' requested here
std::function<void()> f2 = std::bind(f, std::ref(i));
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__functional_base:365:31: note: candidate constructor not viable: requires single argument '__f', but no arguments were provided
_LIBCPP_INLINE_VISIBILITY reference_wrapper(type& __f) _NOEXCEPT : __f_(&__f) {}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__functional_base:367:14: note: candidate constructor not viable: requires 1 argument, but 0 were provided
private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__functional_base:354:24: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
class _LIBCPP_TYPE_VIS reference_wrapper
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__functional_base:354:24: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided
1 error generated.
The bug is fixed in version 5.1