Related
I am trying to create a map between a particle and the location it is on. Essentially, I want my map M[loc] = p where loc is a std::vector <int> and p is a Particle.
This is the code that I have:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
class Particle{
public:
std::vector <int> coords; // the coordinates of the particles
std::string ptype;
int orientation;
bool operator<(const Particle& rhs)const{
return coords < rhs.coords;
}
// constructor
Particle (std::vector <int> crds, std::string type_, int orientation_): coords (crds), ptype (type_), orientation (orientation_){
}
// destructor
~Particle(){
}
Particle( const Particle &other); // copy constructor
// print location of the particle
void printCoords();
};
int main(int argc, char* argv[]){
std::map <std::vector <int>, Particle> OccupancyMap;
Particle p ({0,0,0}, "monomer", 0);
std::cout << p.ptype;
Particle pdash = p;
OccupancyMap[p.coords] = pdash ;
}
when I try to compile this code, I get a massive error message:
In file included from main.cpp:1:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/iostream:37:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ios:215:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale:14:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/string:506:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/string_view:175:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__string:57:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/algorithm:643:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/memory:677:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/tuple:1401:7: error: no matching constructor for initialization of 'Particle'
second(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/utility:507:11: note: in instantiation of function template specialization 'std::__1::pair<const std::__1::vector<int>, Particle>::pair<const std::__1::vector<int> &, 0>' requested here
: pair(__pc, __first_args, __second_args,
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/memory:1684:31: note: in instantiation of function template specialization 'std::__1::pair<const std::__1::vector<int>, Particle>::pair<const std::__1::vector<int> &>' requested here
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/memory:1562:21: note: in instantiation of function template specialization 'std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<std::__1::vector<int>, Particle>, void *>>::construct<std::__1::pair<const std::__1::vector<int>, Particle>, const std::__1::piecewise_construct_t &, std::__1::tuple<const std::__1::vector<int> &>, std::__1::tuple<>>' requested here
__a.construct(__p, _VSTD::forward<_Args>(__args)...);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/memory:1413:14: note: in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<std::__1::vector<int>, Particle>, void *>>>::__construct<std::__1::pair<const std::__1::vector<int>, Particle>, const std::__1::piecewise_construct_t &, std::__1::tuple<const std::__1::vector<int> &>, std::__1::tuple<>>' requested here
{__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__tree:2194:20: note: in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<std::__1::vector<int>, Particle>, void *>>>::construct<std::__1::pair<const std::__1::vector<int>, Particle>, const std::__1::piecewise_construct_t &, std::__1::tuple<const std::__1::vector<int> &>, std::__1::tuple<>>' requested here
__node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__tree:2139:29: note: in instantiation of function template specialization 'std::__1::__tree<std::__1::__value_type<std::__1::vector<int>, Particle>, std::__1::__map_value_compare<std::__1::vector<int>, std::__1::__value_type<std::__1::vector<int>, Particle>, std::__1::less<std::__1::vector<int>>, true>, std::__1::allocator<std::__1::__value_type<std::__1::vector<int>, Particle>>>::__construct_node<const std::__1::piecewise_construct_t &, std::__1::tuple<const std::__1::vector<int> &>, std::__1::tuple<>>' requested here
__node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/map:1521:20: note: in instantiation of function template specialization 'std::__1::__tree<std::__1::__value_type<std::__1::vector<int>, Particle>, std::__1::__map_value_compare<std::__1::vector<int>, std::__1::__value_type<std::__1::vector<int>, Particle>, std::__1::less<std::__1::vector<int>>, true>, std::__1::allocator<std::__1::__value_type<std::__1::vector<int>, Particle>>>::__emplace_unique_key_args<std::__1::vector<int>, const std::__1::piecewise_construct_t &, std::__1::tuple<const std::__1::vector<int> &>, std::__1::tuple<>>' requested here
return __tree_.__emplace_unique_key_args(__k,
^
main.cpp:51:15: note: in instantiation of member function 'std::__1::map<std::__1::vector<int>, Particle, std::__1::less<std::__1::vector<int>>, std::__1::allocator<std::__1::pair<const std::__1::vector<int>, Particle>>>::operator[]' requested here
OccupancyMap[p.coords] = pdash ;
^
main.cpp:35:5: note: candidate constructor not viable: requires single argument 'other', but no arguments were provided
Particle( const Particle &other); // copy constructor for some reason i dont know
^
main.cpp:26:5: note: candidate constructor not viable: requires 3 arguments, but 0 were provided
Particle (std::vector <int> crds, std::string type_, int orientation_): coords (crds), ptype (type_), orientation (orientation_){
^
1 error generated.
I don't quite understand what is going on here. What is the nature of the error here? What concept is it pointing towards?
I would appreciate any advice you have for me. I apologize if this is an obvious question.
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.
I have following code in C++ and it works perfectly fine on GCC, MSVC2015, Android NDK, ...
class JsonLongText {
private:
enum JsonToken {
JT_OBJECT,
JT_ARRAY,
JT_VARIABLE,
JT_EMPTY
};
typedef std::vector<JsonToken> JsonTokenStack;
JsonTokenStack m_OpenTokens;
....
JsonLongText() : m_IgnoreEmptyItems(true), m_RequireItemSeparator(false), m_PrettyPrint(false) {}
}
I sucessfully compiled it on MacOS 10.13 from commandline with XCode 9.2 as well. But after my system was upgraded from 10.13 to 10.13.4 and Xcode to version 9.3. I'm facing following errors:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:643:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2141:9: error: no matching constructor for
initialization of '__compressed_pair_elem<JsonLongText::JsonToken *, 0>'
: _Base1(std::forward<_Tp>(__t)), _Base2() {}
^ ~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:423:7: note: in instantiation of function template
specialization 'std::__1::__compressed_pair<JsonLongText::JsonToken *, std::__1::allocator<JsonLongText::JsonToken> >::__compressed_pair<long, true>'
requested here
__end_cap_(nullptr)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:473:5: note: in instantiation of member function
'std::__1::__vector_base<JsonLongText::JsonToken, std::__1::allocator<JsonLongText::JsonToken> >::__vector_base' requested here
vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
^
../Include/JsonLongText.h:162:2: note: in instantiation of member function 'std::__1::vector<JsonLongText::JsonToken,
std::__1::allocator<JsonLongText::JsonToken> >::vector' requested here
JsonLongText() : m_IgnoreEmptyItems(true), m_RequireItemSeparator(false), m_PrettyPrint(false) {}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2037:8: note: candidate constructor
(the implicit copy constructor) not viable: no known conversion from 'long' to 'const std::__1::__compressed_pair_elem<JsonLongText::JsonToken *, 0,
false>' for 1st argument
struct __compressed_pair_elem {
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2037:8: note: candidate constructor
(the implicit move constructor) not viable: no known conversion from 'long' to 'std::__1::__compressed_pair_elem<JsonLongText::JsonToken *, 0, false>'
for 1st argument
struct __compressed_pair_elem {
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2050:3: note: candidate template ignored:
substitution failure [with _Up = long, $1 = void]
__compressed_pair_elem(_Up&& __u)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2055:3: note: candidate constructor template not
viable: requires 3 arguments, but 1 was provided
__compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2043:39: note: candidate constructor not viable:
requires 0 arguments, but 1 was provided
_LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2141:9: error: no matching constructor for
initialization of '__compressed_pair_elem<unsigned long *, 0>'
: _Base1(std::forward<_Tp>(__t)), _Base2() {}
^ ~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:423:7: note: in instantiation of function template
specialization 'std::__1::__compressed_pair<unsigned long *, std::__1::allocator<unsigned long> >::__compressed_pair<long, true>' requested here
__end_cap_(nullptr)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:473:5: note: in instantiation of member function
'std::__1::__vector_base<unsigned long, std::__1::allocator<unsigned long> >::__vector_base' requested here
vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
^
../Include/JsonLongText.h:162:2: note: in instantiation of member function 'std::__1::vector<unsigned long, std::__1::allocator<unsigned long> >::vector'
requested here
JsonLongText() : m_IgnoreEmptyItems(true), m_RequireItemSeparator(false), m_PrettyPrint(false) {}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2037:8: note: candidate constructor
(the implicit copy constructor) not viable: no known conversion from 'long' to 'const std::__1::__compressed_pair_elem<unsigned long *, 0, false>' for
1st argument
struct __compressed_pair_elem {
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2037:8: note: candidate constructor
(the implicit move constructor) not viable: no known conversion from 'long' to 'std::__1::__compressed_pair_elem<unsigned long *, 0, false>' for 1st
argument
struct __compressed_pair_elem {
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2050:3: note: candidate template ignored:
substitution failure [with _Up = long, $1 = void]
__compressed_pair_elem(_Up&& __u)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2055:3: note: candidate constructor template not
viable: requires 3 arguments, but 1 was provided
__compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2043:39: note: candidate constructor not viable:
requires 0 arguments, but 1 was provided
_LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
I tried to replace whole directory /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1 with files from another Mac where is XCode 9.2. With these files the build of my project finishes sucessfully. It looks like libc++ in the current version of XCode is corrupted.
So, it's possible to downgrade version of XCode or to upgrade libc++?
EDIT:
Similar errors crop up when try to use regex. Following code (copied from cppreference):
// Simple regular expression matching
std::string fnames[] = {"foo.txt", "bar.txt", "baz.dat", "zoidberg"};
std::regex txt_regex("[a-z]+\\.txt");
for (const auto &fname : fnames) {
std::cout << fname << ": " << std::regex_match(fname, txt_regex) << '\n';
}
compile command:
g++ -std=c++11 -DSTATIC_LIB -g -I . -I ../../../../openssl/iOS/include -o out/Expression.o -c Expression.cpp
cause:
In file included from Expression.cpp:113:
In file included from ./StdAfx.h:23:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:643:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2051:9: error: cannot initialize a member subobject
of type 'std::__1::sub_match<const char *> *' with an rvalue of type 'long'
: __value_(_VSTD::forward<_Up>(__u)){};
^ ~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2151:9: note: in instantiation of function template
specialization 'std::__1::__compressed_pair_elem<std::__1::sub_match<const char *> *, 0, false>::__compressed_pair_elem<long, void>' requested here
: _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:432:7: note: in instantiation of function template
specialization 'std::__1::__compressed_pair<std::__1::sub_match<const char *> *, std::__1::allocator<std::__1::sub_match<const char *> >
>::__compressed_pair<long, const std::__1::allocator<std::__1::sub_match<const char *> > &>' requested here
__end_cap_(nullptr, __a)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:485:11: note: in instantiation of member function
'std::__1::__vector_base<std::__1::sub_match<const char *>, std::__1::allocator<std::__1::sub_match<const char *> > >::__vector_base' requested here
: __base(__a)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:5355:7: note: in instantiation of member function
'std::__1::vector<std::__1::sub_match<const char *>, std::__1::allocator<std::__1::sub_match<const char *> > >::vector' requested here
: __matches_(__a),
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2988:34: note: in instantiation of member function
'std::__1::match_results<const char *, std::__1::allocator<std::__1::sub_match<const char *> > >::match_results' requested here
match_results<const _CharT*> __m;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2978:5: note: (skipping 4 contexts in backtrace; use
-ftemplate-backtrace-limit=0 to see all)
__lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s, unsigned __mexp)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:4098:35: note: in instantiation of function template
specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_term<const char *>' requested here
_ForwardIterator __temp = __parse_term(__first, __last);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:4074:31: note: in instantiation of function template
specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_alternative<const char *>' requested here
_ForwardIterator __temp = __parse_alternative(__first, __last);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:3026:19: note: in instantiation of function template
specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_ecma_exp<const char *>' requested here
__first = __parse_ecma_exp(__first, __last);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2522:10: note: in instantiation of function template
specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse<const char *>' requested here
{__parse(__p, __p + __traits_.length(__p));}
^
Expression.cpp:3356:16: note: in instantiation of member function 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::basic_regex' requested here
std::regex txt_regex("[a-z]+\\.txt");
^
In file included from Expression.cpp:113:
In file included from ./StdAfx.h:23:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:643:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2051:9: error: cannot initialize a member subobject
of type 'std::__1::__state<char> *' with an rvalue of type 'long'
: __value_(_VSTD::forward<_Up>(__u)){};
^ ~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2141:9: note: in instantiation of function template
specialization 'std::__1::__compressed_pair_elem<std::__1::__state<char> *, 0, false>::__compressed_pair_elem<long, void>' requested here
: _Base1(std::forward<_Tp>(__t)), _Base2() {}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:423:7: note: in instantiation of function template
specialization 'std::__1::__compressed_pair<std::__1::__state<char> *, std::__1::allocator<std::__1::__state<char> > >::__compressed_pair<long, true>'
requested here
__end_cap_(nullptr)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:473:5: note: in instantiation of member function
'std::__1::__vector_base<std::__1::__state<char>, std::__1::allocator<std::__1::__state<char> > >::__vector_base' requested here
vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:5536:21: note: in instantiation of member function
'std::__1::vector<std::__1::__state<char>, std::__1::allocator<std::__1::__state<char> > >::vector' requested here
vector<__state> __states;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2990:29: note: in instantiation of function template
specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__match_at_start_ecma<std::__1::allocator<std::__1::sub_match<const char *>
> >' requested here
bool __matched = __exp_.__match_at_start_ecma(
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2978:5: note: (skipping 4 contexts in backtrace; use
-ftemplate-backtrace-limit=0 to see all)
__lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s, unsigned __mexp)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:4098:35: note: in instantiation of function template
specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_term<const char *>' requested here
_ForwardIterator __temp = __parse_term(__first, __last);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:4074:31: note: in instantiation of function template
specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_alternative<const char *>' requested here
_ForwardIterator __temp = __parse_alternative(__first, __last);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:3026:19: note: in instantiation of function template
specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_ecma_exp<const char *>' requested here
__first = __parse_ecma_exp(__first, __last);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2522:10: note: in instantiation of function template
specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse<const char *>' requested here
{__parse(__p, __p + __traits_.length(__p));}
^
Expression.cpp:3356:16: note: in instantiation of member function 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::basic_regex' requested here
std::regex txt_regex("[a-z]+\\.txt");
^
In file included from Expression.cpp:113:
In file included from ./StdAfx.h:23:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:643:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2141:9: error: no matching constructor for
initialization of '__compressed_pair_elem<std::__1::sub_match<const char *> *, 0>'
: _Base1(std::forward<_Tp>(__t)), _Base2() {}
^ ~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:423:7: note: in instantiation of function template
specialization 'std::__1::__compressed_pair<std::__1::sub_match<const char *> *, std::__1::allocator<std::__1::sub_match<const char *> >
>::__compressed_pair<long, true>' requested here
__end_cap_(nullptr)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:473:5: note: in instantiation of member function
'std::__1::__vector_base<std::__1::sub_match<const char *>, std::__1::allocator<std::__1::sub_match<const char *> > >::__vector_base' requested here
vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:1326:5: note: in instantiation of member function
'std::__1::vector<std::__1::sub_match<const char *>, std::__1::allocator<std::__1::sub_match<const char *> > >::vector' requested here
__state()
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:5545:28: note: in instantiation of member function
'std::__1::__state<char>::__state' requested here
__states.push_back(__state());
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2990:29: note: in instantiation of function template
specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__match_at_start_ecma<std::__1::allocator<std::__1::sub_match<const char *>
> >' requested here
bool __matched = __exp_.__match_at_start_ecma(
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2978:5: note: (skipping 4 contexts in backtrace; use
-ftemplate-backtrace-limit=0 to see all)
__lookahead(const basic_regex<_CharT, _Traits>& __exp, bool __invert, __node<_CharT>* __s, unsigned __mexp)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:4098:35: note: in instantiation of function template
specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_term<const char *>' requested here
_ForwardIterator __temp = __parse_term(__first, __last);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:4074:31: note: in instantiation of function template
specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_alternative<const char *>' requested here
_ForwardIterator __temp = __parse_alternative(__first, __last);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:3026:19: note: in instantiation of function template
specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse_ecma_exp<const char *>' requested here
__first = __parse_ecma_exp(__first, __last);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/regex:2522:10: note: in instantiation of function template
specialization 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::__parse<const char *>' requested here
{__parse(__p, __p + __traits_.length(__p));}
^
Expression.cpp:3356:16: note: in instantiation of member function 'std::__1::basic_regex<char, std::__1::regex_traits<char> >::basic_regex' requested here
std::regex txt_regex("[a-z]+\\.txt");
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2037:8: note: candidate constructor
(the implicit copy constructor) not viable: no known conversion from 'long' to 'const std::__1::__compressed_pair_elem<std::__1::sub_match<const char
*> *, 0, false>' for 1st argument
struct __compressed_pair_elem {
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2037:8: note: candidate constructor
(the implicit move constructor) not viable: no known conversion from 'long' to 'std::__1::__compressed_pair_elem<std::__1::sub_match<const char *> *,
0, false>' for 1st argument
struct __compressed_pair_elem {
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2050:3: note: candidate template ignored:
substitution failure [with _Up = long, $1 = void]
__compressed_pair_elem(_Up&& __u)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2055:3: note: candidate constructor template not
viable: requires 3 arguments, but 1 was provided
__compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2043:39: note: candidate constructor not viable:
requires 0 arguments, but 1 was provided
_LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
I can now reproduce your exact regex error, verbatim, on my machine.
The trick is to redefine nullptr to 0u as a preprocessor symbol, before including the vector standard library header (which regex depends on):
#define nullptr 0l
#include <regex>
#include <iostream>
int main ()
{
std::string fnames[] = {"foo.txt", "bar.txt", "baz.dat", "zoidberg"};
std::regex txt_regex("[a-z]+\\.txt");
for (const auto &fname : fnames)
std::cout << fname << ": " << std::regex_match(fname, txt_regex) << '\n';
}
In light of the above, I now think your issue is a case of header preprocessor namespace pollution: some header, somewhere in the inclusion chain of your failing translation unit, likely defines a nullptr macro this way, which causes errors with the implementation of the standard library shipped with Xcode 9.3, but (I speculate) not with other implementations, for some reason.
I would suggest inspecting the preprocessor output using g++ -E, and check on what becomes of the offending code at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/vector:432, where I believe the errors are being initially triggered.
Alternatively, you can try always including vector first in your translation units. I suspect this would allow you to dodge the issue, while you keep looking for the root cause.
Good luck!
I just had the problem,xcode version 12.4,carefully check my code and find that there are a define: # define nullptr (NULL).When removing this code, the build succeeds again.
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
I am passing a vector of std::unique_ptr of objects A as a parameter to a constructor of objects Obj. This is working when I use the std::move syntax as is shown below. However, if I add another object to the constructor's parameter list (the mpq_class i in the below code) I get an error message that reads
error: call to implicitly-deleted copy constructor of 'std::__1::unique_ptr<A, std::__1::default_delete<A> >'
on OS X and
error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Interface; _Dp = std::default_delete<Interface>]’
on a linux system.
In the function getVector() the vector of unique_ptr is created, then passed to the Obj constructor and the objects are added to another vector and returned.
My question is why the code tries to call a copy constructor if I use std::move for the unique_ptrs and how to get rid of it!
The code that produces the error is shown below.
// g++ -Wall -O3 -std=c++1y -lgmpxx -lgmp
#include <vector>
#include <gmpxx.h>
#include <gmp.h>
#include <memory>
#include <iostream>
class A {
public:
A( int y ) : x(y) {}
int x;
};
class Obj {
public:
Obj( mpq_class i, std::vector<std::unique_ptr<A> > v ) : number(i), cont( std::move(v) ) {}
mpq_class number;
std::vector<std::unique_ptr<A> > cont;
};
std::vector<Obj> getVector()
{
std::vector<Obj> result;
int M = 3, N = 5;
for( int mm = 0; mm < M; mm++ )
{
mpq_class rational;
std::vector<std::unique_ptr<A> > abstractObjectsForConstructor;
for(int nn = 0; nn < N; nn++ )
{
mpq_class r(mm,nn);
rational = r;
abstractObjectsForConstructor.push_back( std::make_unique<A>(nn) );
}
result.emplace_back( rational, std::move(abstractObjectsForConstructor) );
}
return result;
}
int main()
{
std::vector<Obj> vec = getVector();
for( unsigned int ii = 0; ii < vec.size(); ii++ )
{
for( unsigned int jj = 0; jj < vec[ii].cont.size(); jj++ )
{
std::cout << vec[ii].cont[jj]->x << '\t' << std::endl;
}
}
return 0;
}
The whole error message reads:
In file included from vector_unique.cpp:2:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:628:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1645:31: error:
call to implicitly-deleted copy constructor of 'std::__1::unique_ptr<A, std::__1::default_delete<A> >'
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1572:18: note:
in instantiation of function template specialization 'std::__1::allocator<std::__1::unique_ptr<A,
std::__1::default_delete<A> > >::construct<std::__1::unique_ptr<A, std::__1::default_delete<A> >,
std::__1::unique_ptr<A, std::__1::default_delete<A> > &>' requested here
{__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1453:14: note:
in instantiation of function template specialization
'std::__1::allocator_traits<std::__1::allocator<std::__1::unique_ptr<A, std::__1::default_delete<A> > >
>::__construct<std::__1::unique_ptr<A, std::__1::default_delete<A> >, std::__1::unique_ptr<A,
std::__1::default_delete<A> > &>' requested here
{__construct(__has_construct<allocator_type, pointer, _Args...>(),
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:1005:25: note:
in instantiation of function template specialization
'std::__1::allocator_traits<std::__1::allocator<std::__1::unique_ptr<A, std::__1::default_delete<A> > >
>::construct<std::__1::unique_ptr<A, std::__1::default_delete<A> >, std::__1::unique_ptr<A,
std::__1::default_delete<A> > &>' requested here
__alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:1203:9: note:
in instantiation of function template specialization 'std::__1::vector<std::__1::unique_ptr<A,
std::__1::default_delete<A> >, std::__1::allocator<std::__1::unique_ptr<A, std::__1::default_delete<A> > >
>::__construct_at_end<std::__1::unique_ptr<A, std::__1::default_delete<A> > *>' requested here
__construct_at_end(__x.__begin_, __x.__end_);
^
vector_unique.cpp:15:8: note: in instantiation of member function 'std::__1::vector<std::__1::unique_ptr<A,
std::__1::default_delete<A> >, std::__1::allocator<std::__1::unique_ptr<A, std::__1::default_delete<A> > >
>::vector' requested here
class Obj {
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1572:18: note:
(skipping 2 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
{__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1535:17: note:
in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<Obj>
>::construct<Obj, const Obj &>' requested here
construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:874:21: note:
in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<Obj>
>::__construct_backward<Obj *>' requested here
__alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:1621:5: note:
in instantiation of member function 'std::__1::vector<Obj, std::__1::allocator<Obj>
>::__swap_out_circular_buffer' requested here
__swap_out_circular_buffer(__v);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:1639:9: note:
in instantiation of function template specialization 'std::__1::vector<Obj, std::__1::allocator<Obj>
>::__emplace_back_slow_path<__gmp_expr<mpq_t, mpq_t> &, std::__1::vector<std::__1::unique_ptr<A,
std::__1::default_delete<A> >, std::__1::allocator<std::__1::unique_ptr<A, std::__1::default_delete<A> > > > >'
requested here
__emplace_back_slow_path(_VSTD::forward<_Args>(__args)...);
^
vector_unique.cpp:40:11: note: in instantiation of function template specialization 'std::__1::vector<Obj,
std::__1::allocator<Obj> >::emplace_back<__gmp_expr<mpq_t, mpq_t> &, std::__1::vector<std::__1::unique_ptr<A,
std::__1::default_delete<A> >, std::__1::allocator<std::__1::unique_ptr<A, std::__1::default_delete<A> > > > >'
requested here
result.emplace_back( rational, std::move(abstractObjectsForConstructor) );
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2515:31: note:
copy constructor is implicitly deleted because 'unique_ptr<A, std::__1::default_delete<A> >' has a
user-declared move constructor
_LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
^
1 error generated.
Culprit is mpq_class. With simple dummy version it compiles fine (I commented out headers for gmp) :
struct mpq_class {
mpq_class() {}
mpq_class( int, int );
};
However, when you make that class not movable:
struct mpq_class {
mpq_class() {}
mpq_class( int, int );
mpq_class( const mpq_class & ) {}
mpq_class( mpq_class && ) = delete;
mpq_class &operator=( const mpq_class & ) {return *this;}
};
Your problem comes back.
So issue that if mpq_class is not movable then Obj is not movable either and usage of emplace_back implicitly generates code to copy Obj in case of relocation which fails to compile because std::unique_ptr<A> is not copyable.
[wrong, see below]
The problem is with the definition of the Obj constructor. Its second argument is passed by value, which results in a copy being made. The fact that you use std::move in the initializer list does not change this fact.
The remedy is to use an rvalue reference for the second argument of the Obj constructor.