XCode 9.3 upgrade or replace libc++ - c++

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.

Related

Moving all Elements in a std::vector not working as expected

I'm trying to move the whole vector to another variable.
The following code block does not compile.
I thought that the call to std::move would force to use the Move-Constructor. Instead it seems like std::vector is using the Copy-Constructor that is deleted.
What's wrong here? How can I move all elements without copying them?
#include <vector>
class TheClass {
public:
TheClass(const TheClass&) = delete;
TheClass(TheClass&&) = default;
};
void fun2(const std::vector<TheClass> data) {
std::vector<TheClass> a = std::move(data);
a.size();
}
leads to:
clang++ -o /cplayground/cplayground /cplayground/code.cpp -I/cplayground/include -L/cplayground/lib -std=c++20 -O0 -Wall -no-pie -lm -pthread
In file included from /cplayground/code.cpp:1:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/vector:66:
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_uninitialized.h:137:7: error: static_assert failed due to requirement 'is_constructible<TheClass, const TheClass &>::value' "result type must be constructible from value type of input range"
static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_uninitialized.h:325:19: note: in instantiation of function template specialization 'std::uninitialized_copy<__gnu_cxx::__normal_iterator<const TheClass *, std::vector<TheClass, std::allocator<TheClass> > >, TheClass *>' requested here
{ return std::uninitialized_copy(__first, __last, __result); }
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_vector.h:558:9: note: in instantiation of function template specialization 'std::__uninitialized_copy_a<__gnu_cxx::__normal_iterator<const TheClass *, std::vector<TheClass, std::allocator<TheClass> > >, TheClass *, TheClass>' requested here
std::__uninitialized_copy_a(__x.begin(), __x.end(),
^
/cplayground/code.cpp:10:29: note: in instantiation of member function 'std::vector<TheClass, std::allocator<TheClass> >::vector' requested here
std::vector<TheClass> a = std::move(data);
^
In file included from /cplayground/code.cpp:1:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/vector:62:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_algo.h:62:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_tempbuf.h:60:
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_construct.h:109:38: error: call to deleted constructor of 'TheClass'
{ ::new(static_cast<void*>(__p)) _Tp(std::forward<_Args>(__args)...); }
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_uninitialized.h:91:8: note: in instantiation of function template specialization 'std::_Construct<TheClass, const TheClass &>' requested here
std::_Construct(std::__addressof(*__cur), *__first);
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_uninitialized.h:150:2: note: in instantiation of function template specialization 'std::__uninitialized_copy<false>::__uninit_copy<__gnu_cxx::__normal_iterator<const TheClass *, std::vector<TheClass, std::allocator<TheClass> > >, TheClass *>' requested here
__uninit_copy(__first, __last, __result);
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_uninitialized.h:325:19: note: in instantiation of function template specialization 'std::uninitialized_copy<__gnu_cxx::__normal_iterator<const TheClass *, std::vector<TheClass, std::allocator<TheClass> > >, TheClass *>' requested here
{ return std::uninitialized_copy(__first, __last, __result); }
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_vector.h:558:9: note: in instantiation of function template specialization 'std::__uninitialized_copy_a<__gnu_cxx::__normal_iterator<const TheClass *, std::vector<TheClass, std::allocator<TheClass> > >, TheClass *, TheClass>' requested here
std::__uninitialized_copy_a(__x.begin(), __x.end(),
^
/cplayground/code.cpp:10:29: note: in instantiation of member function 'std::vector<TheClass, std::allocator<TheClass> >::vector' requested here
std::vector<TheClass> a = std::move(data);
^
/cplayground/code.cpp:5:3: note: 'TheClass' has been explicitly marked deleted here
TheClass(const TheClass&) = delete;
^
2 errors generated.
You can't move something that is const (I mean, you can call std::move on it, but it won't allow you to move things). Remove that qualifier and (maybe) pass a rvalue reference:
void fun2(std::vector<TheClass>&& data)
{
std::vector<TheClass> a = std::move(data);
a.size();
}
Then :
std::vector<TheClass> vec;
fun2(std::move(vec));
Note that you may omit that &&(see https://godbolt.org/z/5M1f4j66r).

std::any containing std::tuple<std::any> fails to compile

I realize nested std::any's are a bad idea. Nevertheless, I encountered something that's making me scratch my head and I'm just trying to understand why the compiler is choking. Consider the following line of code (assuming that the arg variable is a std::any containing another std::any):
auto result = std::any_cast<std::any>(arg);
That line compiles just fine. Now, consider this line, where I have a std::any containing a std::tuple that contains another std::any:
auto result = std::any_cast<std::tuple<std::any>>(arg);
Now, the compiler blows up. These are the errors I get:
In file included from /home/james/git/trogdor-pp/src/core/event/triggers/luaeventtrigger.cpp:1:
In file included from /home/james/git/trogdor-pp/src/core/include/trogdor/game.h:5:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/any:37:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/new:40:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/exception:144:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/nested_exception.h:40:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/move.h:55:
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/type_traits:132:31: error: no member named 'value' in 'std::is_copy_constructible<std::tuple<std::any> >'
: public conditional<_B1::value, _B2, _B1>::type
~~~~~^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/any:170:17: note: in instantiation of template class 'std::__and_<std::is_copy_constructible<std::tuple<std::any> >, std::is_constructible<std::tuple<std::any>, const std::tuple<std::any> &> >' requested here
enable_if<__and_<is_copy_constructible<_Tp>,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/any:175:5: note: in instantiation of template type alias '__any_constructible' requested here
using __any_constructible_t =
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/any:181:56: note: in instantiation of template type alias '__any_constructible_t' requested here
__any_constructible_t<_Tp, _ValueType&&> = true,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/any:183:7: note: while substituting prior template arguments into non-type template parameter [with _ValueType = const std::tuple<std::any> &, _Tp = std::tuple<std::any>, _Mgr = std::any::_Manager_external<std::tuple<std::any> >]
any(_ValueType&& __value)
^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/type_traits:884:56: note: while substituting deduced template arguments into function template 'any' [with _ValueType = const std::tuple<std::any> &, _Tp = (no value), _Mgr = (no value), $3 = (no value), $4 = (no value)]
: public __bool_constant<__is_constructible(_Tp, _Args...)>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/type_traits:884:32: note: (skipping 2 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
: public __bool_constant<__is_constructible(_Tp, _Args...)>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/type_traits:908:14: note: in instantiation of template class 'std::__is_copy_constructible_impl<std::tuple<std::any>, true>' requested here
: public __is_copy_constructible_impl<_Tp>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/type_traits:109:14: note: in instantiation of template class 'std::is_copy_constructible<std::tuple<std::any> >' requested here
: public conditional<_B1::value, _B1, _B2>::type
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/any:358:16: note: in instantiation of template class 'std::__or_<std::is_reference<std::tuple<std::any> >, std::is_copy_constructible<std::tuple<std::any> > >' requested here
{ return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/any:481:26: note: in instantiation of function template specialization 'std::any::__is_valid_cast<std::tuple<std::any> >' requested here
static_assert(any::__is_valid_cast<_ValueType>(),
^
/home/james/git/trogdor-pp/src/core/event/triggers/luaeventtrigger.cpp:42:32: note: in instantiation of function template specialization 'std::any_cast<std::tuple<std::any> >' requested here
auto arg = std::any_cast<std::tuple<std::any>>(a);
^
In file included from /home/james/git/trogdor-pp/src/core/event/triggers/luaeventtrigger.cpp:1:
In file included from /home/james/git/trogdor-pp/src/core/include/trogdor/game.h:5:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/any:37:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/new:40:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/exception:144:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/nested_exception.h:40:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/move.h:55:
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/type_traits:137:31: error: no member named 'value' in 'std::is_copy_constructible<std::tuple<std::any> >'
: public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type
~~~~~^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/type_traits:150:37: note: in instantiation of template class 'std::__and_<std::is_copy_constructible<std::tuple<std::any> >, std::__not_<std::is_constructible<std::tuple<std::any>, const std::tuple<std::any> &> >, std::__not_<std::__is_in_place_type<std::tuple<std::any> > > >' requested here
inline constexpr bool __and_v = __and_<_Bn...>::value;
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/any:192:27: note: in instantiation of variable template specialization 'std::__and_v<std::is_copy_constructible<std::tuple<std::any> >, std::__not_<std::is_constructible<std::tuple<std::any>, const std::tuple<std::any> &> >, std::__not_<std::__is_in_place_type<std::tuple<std::any> > > >' requested here
enable_if_t<__and_v<is_copy_constructible<_Tp>,
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/any:196:7: note: while substituting prior template arguments into non-type template parameter [with _ValueType = const std::tuple<std::any> &, _Tp = std::tuple<std::any>, _Mgr = std::any::_Manager_external<std::tuple<std::any> >]
any(_ValueType&& __value)
^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/type_traits:884:56: note: while substituting deduced template arguments into function template 'any' [with _ValueType = const std::tuple<std::any> &, _Tp = (no value), _Mgr = (no value), $3 = (no value)]
: public __bool_constant<__is_constructible(_Tp, _Args...)>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/type_traits:884:32: note: while substituting deduced template arguments into function template 'tuple' [with _Dummy = (no value), $1 = (no value)]
: public __bool_constant<__is_constructible(_Tp, _Args...)>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/type_traits:902:14: note: (skipping 1 context in backtrace; use -ftemplate-backtrace-limit=0 to see all)
: public is_constructible<_Tp, const _Tp&>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/type_traits:908:14: note: in instantiation of template class 'std::__is_copy_constructible_impl<std::tuple<std::any>, true>' requested here
: public __is_copy_constructible_impl<_Tp>
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/type_traits:109:14: note: in instantiation of template class 'std::is_copy_constructible<std::tuple<std::any> >' requested here
: public conditional<_B1::value, _B1, _B2>::type
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/any:358:16: note: in instantiation of template class 'std::__or_<std::is_reference<std::tuple<std::any> >, std::is_copy_constructible<std::tuple<std::any> > >' requested here
{ return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/any:481:26: note: in instantiation of function template specialization 'std::any::__is_valid_cast<std::tuple<std::any> >' requested here
static_assert(any::__is_valid_cast<_ValueType>(),
^
/home/james/git/trogdor-pp/src/core/event/triggers/luaeventtrigger.cpp:42:32: note: in instantiation of function template specialization 'std::any_cast<std::tuple<std::any> >' requested here
auto arg = std::any_cast<std::tuple<std::any>>(a);
I'm having a hard time unpacking all of that and figuring out what's wrong and how I can fix it.
EDIT: we've discovered in the comments that this code does not compile under G++ 9 and below, but compiles on G++ 10 and above, and also fails on clang 10 and below, but compiles on clang 11 and above. Does anybody have any guesses as to why this might be the case?
The key part is error: no member named 'value' in 'std::is_copy_constructible<std::tuple<std::any> >'
This is the compiler's way of complaining that the indicated object, the std::tuple, is not copy-constructible.
Your gcc does not believe, apparently, that std::tuplestd::any is copy-constructible. A tuple, in of itself, should be copy constructible if everything in the tuple is copy-constructible. Given that std::any is C++17, and gcc 9's C++17 support is incomplete, at least incomplete insofar as this part of C++17 goes (gcc 11's feature list still includes some C++17 features).

Type trait on std::reference_wrapper<std::any>

std::reference_wrapper should be copy-constructible, regardless of the contained type (it's just a pointer inside). However, when asserting that using
static_assert(std::is_copy_constructible<std::reference_wrapper<std::any> >::value, "!");
Clang terminates with a compile error from inside the STL, while GCC reports no problems. This is not the case with other types (I tested std::reference_wrapper<int> and std::any directly).
I have an example showing compilation in GCC and Clang at https://godbolt.org/z/7YwkWb.
As far as I understand it, even if the type given to std::is_copy_constructible is not copy-constructible, it should just give a compile-time false, rather than failing compilation.
What is the problem here? Is that a compiler bug or am I missing something?
The compile error from Clang is:
In file included from <source>:1:
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:132:31: error: no member named 'value' in 'std::is_copy_constructible<std::reference_wrapper<std::any> >'
: public conditional<_B1::value, _B2, _B1>::type
~~~~~^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/any:170:17: note: in instantiation of template class 'std::__and_<std::is_copy_constructible<std::reference_wrapper<std::any> >, std::is_constructible<std::reference_wrapper<std::any>, const std::reference_wrapper<std::any> &> >' requested here
enable_if<__and_<is_copy_constructible<_Tp>,
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/any:175:5: note: in instantiation of template type alias '__any_constructible' requested here
using __any_constructible_t =
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/any:181:56: note: in instantiation of template type alias '__any_constructible_t' requested here
__any_constructible_t<_Tp, _ValueType&&> = true,
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/any:183:7: note: while substituting prior template arguments into non-type template parameter [with _ValueType = const std::reference_wrapper<std::any> &, _Tp = std::reference_wrapper<std::any>, _Mgr = std::any::_Manager_internal<std::reference_wrapper<std::any> >]
any(_ValueType&& __value)
^~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:921:56: note: while substituting deduced template arguments into function template 'any' [with _ValueType = const std::reference_wrapper<std::any> &, _Tp = (no value), _Mgr = (no value), $3 = (no value), $4 = (no value)]
: public __bool_constant<__is_constructible(_Tp, _Args...)>
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:933:14: note: in instantiation of template class 'std::is_constructible<std::reference_wrapper<std::any>, const std::reference_wrapper<std::any> &>' requested here
: public is_constructible<_Tp, const _Tp&>
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:939:14: note: in instantiation of template class 'std::__is_copy_constructible_impl<std::reference_wrapper<std::any>, true>' requested here
: public __is_copy_constructible_impl<_Tp>
^
<source>:12:20: note: in instantiation of template class 'std::is_copy_constructible<std::reference_wrapper<std::any> >' requested here
static_assert(std::is_copy_constructible<std::reference_wrapper<std::any> >::value, "!");
^
In file included from <source>:1:
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:137:31: error: no member named 'value' in 'std::is_copy_constructible<std::reference_wrapper<std::any> >'
: public conditional<_B1::value, __and_<_B2, _B3, _Bn...>, _B1>::type
~~~~~^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/any:192:27: note: in instantiation of template class 'std::__and_<std::is_copy_constructible<std::reference_wrapper<std::any> >, std::__not_<std::is_constructible<std::reference_wrapper<std::any>, const std::reference_wrapper<std::any> &> >, std::__not_<std::__is_in_place_type<std::reference_wrapper<std::any> > > >' requested here
enable_if_t<__and_<is_copy_constructible<_Tp>,
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/any:196:7: note: while substituting prior template arguments into non-type template parameter [with _ValueType = const std::reference_wrapper<std::any> &, _Tp = std::reference_wrapper<std::any>, _Mgr = std::any::_Manager_internal<std::reference_wrapper<std::any> >]
any(_ValueType&& __value)
^~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:921:56: note: while substituting deduced template arguments into function template 'any' [with _ValueType = const std::reference_wrapper<std::any> &, _Tp = (no value), _Mgr = (no value), $3 = (no value)]
: public __bool_constant<__is_constructible(_Tp, _Args...)>
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:933:14: note: in instantiation of template class 'std::is_constructible<std::reference_wrapper<std::any>, const std::reference_wrapper<std::any> &>' requested here
: public is_constructible<_Tp, const _Tp&>
^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/type_traits:939:14: note: in instantiation of template class 'std::__is_copy_constructible_impl<std::reference_wrapper<std::any>, true>' requested here
: public __is_copy_constructible_impl<_Tp>
^
<source>:12:20: note: in instantiation of template class 'std::is_copy_constructible<std::reference_wrapper<std::any> >' requested here
static_assert(std::is_copy_constructible<std::reference_wrapper<std::any> >::value, "!");
^
2 errors generated.
Compiler returned: 1
The error you see clearly states at the top that std::is_copy_constructible doesn't have a value member for the specialization you check. Now, the types you pass don't violate the contract of std::is_copy_constructible, so there is no risk of undefined behavior.
Which means only one thing, it's not a compiler issue, but a standard library implementation issue. Clang uses the default installed standard library implementation, which on the system godbolt is running is libstdc++ (the GNU implementation). This evidently causes the issue due to some possible soft incompatibility.
If you specify the -stdlib=libc++ option (libc++ is the LLVM implementation of the standard library) when building with Clang, then it accepts the code fine.

using std::move to move a unique_ptr into a vector

I have been trying to create a unique_ptr, and then move it into a vector of that pointer with push_back(). When I try to do this, I get a long compilation error. I have read multiple questions on this subject, including the section on "Containers of unique_ptr" here: https://eli.thegreenplace.net/2012/06/20/c11-using-unique_ptr-with-standard-library-containers
As well as this StackOverflow question: Why can I not push_back a unique_ptr into a vector?
This is a tiny sample program that will not compile:
// ptrtest.cpp
#include <memory>
#include <vector>
class TestObject{
public:
TestObject(int data): data(data){}
int getData(){return data;}
private:
int data;
};
using namespace std;
int main (int argc, char* argv[]){
vector<unique_ptr<TestObject> > v;
unique_ptr<TestObject> obj = unique_ptr<TestObject>(new TestObject(5));
v.push_back(move(obj));
return 0;
}
This is the compiler error the program gives me:
$ clang++ ptrtest.cpp -otest
In file included from ptrtest.cpp:1:
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1574:36: error:
no matching constructor for initialization of
'std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >'
::new ((void*)__p) _Tp(__a0);
^ ~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:1593:25: note: in
instantiation of function template specialization
'std::__1::allocator_traits<std::__1::allocator<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> > >
>::construct<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> >, std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> > >' requested here
__alloc_traits::construct(this->__alloc(),
^
ptrtest.cpp:16:4: note: in instantiation of member function
'std::__1::vector<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> >,
std::__1::allocator<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> > > >::push_back' requested here
v.push_back(move(obj));
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2482:3: note:
candidate constructor not viable: 1st argument ('const
std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >')
would lose const qualifier
unique_ptr(unique_ptr&);
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2498:3: note:
candidate constructor not viable: no known conversion from 'const
std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >'
to 'std::__1::nullptr_t' for 1st argument
unique_ptr(nullptr_t) : __ptr_(pointer())
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2504:12: note:
candidate constructor not viable: no known conversion from 'const
std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >'
to 'std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject>
>::pointer' (aka 'TestObject *') for 1st argument
explicit unique_ptr(pointer __p)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2516:3: note:
candidate constructor not viable: no known conversion from 'const
std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >'
to '__rv<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> > >' for 1st argument
unique_ptr(__rv<unique_ptr> __u)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2483:35: note:
candidate template ignored: deduced type 'unique_ptr<...>' of 1st
parameter does not match adjusted type 'const unique_ptr<...>' of
argument [with _Up = TestObject, _Ep =
std::__1::default_delete<TestObject>]
template <class _Up, class _Ep> unique_ptr(unique_ptr<_Up, _Ep>&);
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2490:3: note:
candidate constructor not viable: requires 0 arguments, but 1 was provided
unique_ptr() : __ptr_(pointer())
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2535:3: note:
candidate constructor not viable: requires 2 arguments, but 1 was provided
unique_ptr(pointer __p, deleter_type __d)
^
In file included from ptrtest.cpp:2:
/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:1580:21: error:
no matching member function for call to 'construct'
__alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), ...
~~~~~~~~~~~~~~~~^~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:1599:9: note: in
instantiation of function template specialization
'std::__1::vector<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> >,
std::__1::allocator<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> > > >::__push_back_slow_path<const
std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> > >'
requested here
__push_back_slow_path(__x);
^
ptrtest.cpp:16:4: note: in instantiation of member function
'std::__1::vector<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> >,
std::__1::allocator<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> > > >::push_back' requested here
v.push_back(move(obj));
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1572:21: note:
candidate template ignored: substitution failure [with _Tp =
std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >,
_A0 = std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> >]
static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1566:21: note:
candidate function template not viable: requires 2 arguments, but 3 were
provided
static void construct(allocator_type&, _Tp* __p)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1578:21: note:
candidate function template not viable: requires 4 arguments, but 3 were
provided
static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1585:21: note:
candidate function template not viable: requires 5 arguments, but 3 were
provided
static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1677:17: error:
use of undeclared identifier 'construct'
construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD:...
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:898:21: note: in
instantiation of function template specialization
'std::__1::allocator_traits<std::__1::allocator<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> > >
>::__construct_backward<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> > *>' requested here
__alloc_traits::__construct_backward(this->__alloc(), this->__begin_...
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:1582:5: note: in
instantiation of member function
'std::__1::vector<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> >,
std::__1::allocator<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> > > >::__swap_out_circular_buffer'
requested here
__swap_out_circular_buffer(__v);
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:1599:9: note: in
instantiation of function template specialization
'std::__1::vector<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> >,
std::__1::allocator<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> > > >::__push_back_slow_path<const
std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> > >'
requested here
__push_back_slow_path(__x);
^
ptrtest.cpp:16:4: note: in instantiation of member function
'std::__1::vector<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> >,
std::__1::allocator<std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> > > >::push_back' requested here
v.push_back(move(obj));
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1566:21: note:
must qualify identifier to find this declaration in dependent base class
static void construct(allocator_type&, _Tp* __p)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1572:21: note:
must qualify identifier to find this declaration in dependent base class
static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1578:21: note:
must qualify identifier to find this declaration in dependent base class
static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1585:21: note:
must qualify identifier to find this declaration in dependent base class
static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1677:17: error:
no matching function for call to 'construct'
construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD:...
^~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1572:21: note:
candidate template ignored: substitution failure [with _Tp =
std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >,
_A0 = std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> >]
static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1566:21: note:
candidate function template not viable: requires 2 arguments, but 3 were
provided
static void construct(allocator_type&, _Tp* __p)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1578:21: note:
candidate function template not viable: requires 4 arguments, but 3 were
provided
static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1585:21: note:
candidate function template not viable: requires 5 arguments, but 3 were
provided
static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1677:17: error:
no matching function for call to 'construct'
construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD:...
^~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1572:21: note:
candidate template ignored: substitution failure [with _Tp =
std::__1::unique_ptr<TestObject, std::__1::default_delete<TestObject> >,
_A0 = std::__1::unique_ptr<TestObject,
std::__1::default_delete<TestObject> >]
static void construct(allocator_type&, _Tp* __p, const _A0& __a0)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1578:21: note:
candidate function template not viable: requires 4 arguments, but 3 were
provided
static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1585:21: note:
candidate function template not viable: requires 5 arguments, but 3 were
provided
static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1566:21: note:
candidate function template not viable: requires 2 arguments, but 3 were
provided
static void construct(allocator_type&, _Tp* __p)
^
5 errors generated.
My compiler is clang++ and I'm running macOS 10.14.3.
Are you sure you are using c++11?
Prior to c++11 the signature for push_back(...) was:
void push_back (const value_type& val);
So even if you give a temporary it will still use the copy constructor.
In C++11 there is an overload to handle the use of temporary variables to move instead of copy.
Either way, you should consider using .emplace_back(...). This was specifically made for handling temporaries and inline constructing the object so there is no copy.

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