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.
Related
My code use templates heavily. I have a number of overloaded functions
auto operator>>(byte_vector_view&bvv, Type&&) ->byte_vector_view&;
This works fine until unknown by this function family type is passed to function:
bvv >> my_custom;
User should simply see one error message:
There no implementation for operator>>(byte_vector_view&bvv, my_custom_type&)
instead we see a very long manuscript:
how to put this under spoiler?
itm.cpp:184:18: error: invalid operands to binary expression ('byte_vector_view' and 'named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >')
((bvv>>elements),...);
~~~^ ~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/type_traits:4345:23: note: in instantiation of function template specialization 'operator>>(byte_vector_view &, std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > &)::(anonymous class)::operator()<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' requested here
_LIBCPP_INVOKE_RETURN(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...))
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/__config:508:15: note: expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_NAMESPACE
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1375:5: note: while substituting deduced template arguments into function template '__invoke_constexpr' [with _Fp = (lambda at itm.cpp:183:9), _Args = <named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > &>]
_VSTD::__invoke_constexpr(
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/__config:508:15: note: expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_NAMESPACE
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1372:26: note: in instantiation of exception specification for '__apply_tuple_impl<(lambda at itm.cpp:183:9), std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > &, 0>' requested here
constexpr decltype(auto) __apply_tuple_impl(_Fn && __f, _Tuple && __t,
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1384:12: note: in instantiation of function template specialization 'std::__1::__apply_tuple_impl<(lambda at itm.cpp:183:9), std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > &, 0>' requested here
_VSTD::__apply_tuple_impl(
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1382:26: note: in instantiation of exception specification for 'apply<(lambda at itm.cpp:183:9), std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > &>' requested here
constexpr decltype(auto) apply(_Fn && __f, _Tuple && __t)
^
itm.cpp:182:5: note: in instantiation of function template specialization 'std::__1::apply<(lambda at itm.cpp:183:9), std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > &>' requested here
apply(
^
itm.cpp:461:20: note: in instantiation of function template specialization 'operator>><std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > >' requested here
bvv>>tvs;
^
itm.cpp:427:12: note: in instantiation of member function 'make_converter(bool, std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> > &&, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > &&)::Ret::from_byte_vector_view' requested here
struct Ret:converter_virtual{
^
itm.cpp:537:12: note: in instantiation of function template specialization 'make_converter<std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> >, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' requested here
return make_converter(false,forward<Prefix>(prefix),forward<Ts>(values)...);
^
itm.cpp:931:18: note: in instantiation of function template specialization 'make_converter<std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> >, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' requested here
/*response*/make_converter(
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/type_traits:4840:3: note: candidate function template not viable: no known conversion from 'byte_vector_view' to 'std::byte' for 1st argument
operator>> (byte __lhs, _Integer __shift) noexcept
^
itm.cpp:147:19: note: candidate function not viable: no known conversion from 'named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >' to 'uint8_t &' (aka 'unsigned char &') for 2nd argument
byte_vector_view& operator>>(byte_vector_view&bvv, uint8_t&ui8){
^
itm.cpp:151:19: note: candidate function not viable: no known conversion from 'named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >' to 'std::__1::string &' (aka 'basic_string<char, char_traits<char>, allocator<char> > &') for 2nd argument
byte_vector_view& operator>>(byte_vector_view&bvv, string&str){
^
itm.cpp:156:19: note: candidate function not viable: no known conversion from 'named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >' to 'named_value<std::__1::string> &' (aka 'named_value<basic_string<char, char_traits<char>, allocator<char> > > &') for 2nd argument
byte_vector_view& operator>>(byte_vector_view&bvv, named_value<string>&av){
^
itm.cpp:161:6: note: candidate template ignored: requirement 'is_integral_v<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > || is_same_v<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >, ringnet::proto::opcode> || is_same_v<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >, command_address>' was not satisfied [with T = std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >]
auto operator>>(byte_vector_view&bvv, named_value<T>&av) ->enable_if_t<is_integral_v<T> || is_same_v<T,opcode> || is_same_v<T,command_address>, byte_vector_view&>{
^
itm.cpp:181:6: note: candidate template ignored: requirement 'is_tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' was not satisfied [with TupleT = named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >]
auto operator>>(byte_vector_view&bvv, TupleT&tu) ->enable_if_t<is_tuple<TupleT>,byte_vector_view&> {
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:521:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:570:1: note: candidate template ignored: could not match 'basic_istream<char, type-parameter-0-0>' against 'byte_vector_view'
operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:578:1: note: candidate template ignored: could not match 'basic_istream<char, type-parameter-0-0>' against 'byte_vector_view'
operator>>(basic_istream<char, _Traits>& __is, signed char* __s)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:585:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:613:1: note: candidate template ignored: could not match 'basic_istream<char, type-parameter-0-0>' against 'byte_vector_view'
operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:621:1: note: candidate template ignored: could not match 'basic_istream<char, type-parameter-0-0>' against 'byte_vector_view'
operator>>(basic_istream<char, _Traits>& __is, signed char& __c)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:1219:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp&& __x)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:1287:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>& __is,
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:1419:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iomanip:302:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x)
It's hard to read and understand where is the problem exactly and therefore hard to fix the problem
Need more clear explanation
I found one straightforward way to suppress excess output:
template<typename UnknownType>
auto operator>>(byte_vector_view&bvv, UnknownType&&) ->byte_vector_view& {
static_assert(false);
return bvv;
}
With it compiler gives a quiet short error message, too short:
itm.cpp:192:5: error: static_assert failed
static_assert(false);
^ ~~~~~
1 error generated.
I'd like to see in which function the error appeared and a trace how compiler come there:
template<typename UnknownType>
auto operator>>(byte_vector_view&bvv, UnknownType&&) ->byte_vector_view& {
static_assert(false&&__PRETTY_FUNCTION__);
return bvv;
}
itm.cpp:192:5: error: static_assert failed
static_assert(false && __PRETTY_FUNCTION__);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
itm.cpp:184:18: note: in instantiation of function template specialization 'operator>><named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > &>' requested here
((bvv>>elements),...);
^
itm.cpp:461:20: note: in instantiation of function template specialization 'operator>><std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > >' requested here
bvv>>tvs;
^
itm.cpp:427:12: note: in instantiation of member function 'make_converter(bool, std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> > &&, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > &&)::Ret::from_byte_vector_view' requested here
struct Ret:converter_virtual{
^
itm.cpp:537:12: note: in instantiation of function template specialization 'make_converter<std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> >, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' requested here
return make_converter(false,forward<Prefix>(prefix),forward<Ts>(values)...);
^
itm.cpp:931:18: note: in instantiation of function template specialization 'make_converter<std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> >, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' requested here
/*response*/make_converter(
^
1 error generated.
And this is almost looks like I want but Clang++ does not expand __PRETTY_FUNCTION__ in static_assert.
Is there a way to put expanded UnknownType to static_assert's message?
I also tried
static_assert(false,__PRETTY_FUNCTION__);
but it gives
error: expected string literal for diagnostic message in static_assert
#include <type_traits>
template <typename UnknownType>
struct Type_Not_Supported : std::false_type {};
template <typename UnknownType>
auto operator>>(byte_vector_view& bvv, UnknownType&&) -> byte_vector_view& {
static_assert(Type_Not_Supported<UnknownType>{});
return bvv;
}
In Clang, this yields:
prog.cc:10:5: error: static_assert failed due to requirement 'Type_Not_Supported<int>{}'
static_assert(Type_Not_Supported<UnknownType>{});
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:17:9: note: in instantiation of function template specialization 'operator>><int>' requested here
bvv >> 1;
^
1 error generated.
In GCC:
prog.cc: In instantiation of 'byte_vector_view& operator>>(byte_vector_view&, UnknownType&&) [with UnknownType = int]':
prog.cc:17:12: required from here
prog.cc:10:19: error: static assertion failed
10 | static_assert(Type_Not_Supported<UnknownType>{});
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DEMO
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.
When compiling my program with clang++ --std=c++11 file.cpp the line std::unique_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose); throws the error
memdiff.cpp:11:27: error: no matching constructor for initialization of
'std::unique_ptr<FILE>'
std::unique_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2530:31: note:
candidate constructor not viable: no known conversion from 'int (FILE *)'
to 'const std::__1::default_delete<__sFILE>' for 2nd argument
_LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename conditional<
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2537:31: note:
candidate constructor not viable: no known conversion from 'int (FILE *)'
to 'typename remove_reference<deleter_type>::type' (aka
'std::__1::default_delete<__sFILE>') for 2nd argument
_LIBCPP_INLINE_VISIBILITY unique_ptr(pointer __p, typename remove_ref...
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2547:9: note:
candidate template ignored: could not match 'unique_ptr<type-parameter-0-0,
type-parameter-0-1>' against '__sFILE *'
unique_ptr(unique_ptr<_Up, _Ep>&& __u,
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2562:35: note:
candidate template ignored: could not match 'auto_ptr<type-parameter-0-0>'
against '__sFILE *'
_LIBCPP_INLINE_VISIBILITY unique_ptr(auto_ptr<_Up>&& __p,
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2516:49: note:
candidate constructor not viable: requires 1 argument, but 2 were provided
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2522:40: note:
candidate constructor not viable: requires single argument '__p', but 2
arguments were provided
_LIBCPP_INLINE_VISIBILITY explicit unique_ptr(pointer __p) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2543:31: note:
candidate constructor not viable: requires single argument '__u', but 2
arguments were provided
_LIBCPP_INLINE_VISIBILITY unique_ptr(unique_ptr&& __u) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2487:29: note:
candidate constructor (the implicit copy constructor) not viable: requires
1 argument, but 2 were provided
class _LIBCPP_TYPE_VIS_ONLY unique_ptr
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:2510:49: note:
candidate constructor not viable: requires 0 arguments, but 2 were provided
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
^
1 error generated.
If I switch from a unique_ptr to a shared_ptr my program compiles. Why does one constructor work and one not, and how do I fix it?
As correctly pointed out in the first comment: With unique_ptr, the type of the deleter must be specified as the second template parameter.
However, it's supposed to be a function pointer:
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
I have a class as:
#include <memory>
class Object {
std::shared_ptr<void> object_ptr;
public:
Object() {}
template<typename T>
Object(T&& object)
: object_ptr {new T {std::move(object)} } {}
virtual ~Object() {};
};
My main cpp file is:
#include <iostream>
#include "Object.hpp"
class Foo {};
int main() {
Object o {Foo{}};
}
It gives me error:
test/test.cpp:13:20: required from here
include/Object.hpp:24:49: error: could not convert ‘{std::move<Foo&>((* & object))}’ from ‘<brace-enclosed initializer list>’ to ‘Foo’
: object_ptr {new T {std::move(object)} } {}
^
include/Object.hpp:24:49: error: no matching function for call to ‘std::shared_ptr<void>::shared_ptr(<brace-enclosed initializer list>)’
include/Object.hpp:24:49: note: candidates are:
In file included from /usr/include/c++/4.8/memory:82:0,
from include/Object.hpp:7,
from test/test.cpp:2:
/usr/include/c++/4.8/bits/shared_ptr.h:314:2: note: template<class _Alloc, class ... _Args> std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...)
shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
^
/usr/include/c++/4.8/bits/shared_ptr.h:314:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:265:17: note: constexpr std::shared_ptr<_Tp>::shared_ptr(std::nullptr_t) [with _Tp = void; std::nullptr_t = std::nullptr_t]
constexpr shared_ptr(nullptr_t __p) noexcept
^
/usr/include/c++/4.8/bits/shared_ptr.h:265:17: note: no known conversion for argument 1 from ‘<type error>’ to ‘std::nullptr_t’
/usr/include/c++/4.8/bits/shared_ptr.h:257:2: note: template<class _Tp1, class _Del> std::shared_ptr<_Tp>::shared_ptr(std::unique_ptr<_Up, _Ep>&&)
shared_ptr(std::unique_ptr<_Tp1, _Del>&& __r)
^
/usr/include/c++/4.8/bits/shared_ptr.h:257:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:253:2: note: template<class _Tp1> std::shared_ptr<_Tp>::shared_ptr(std::auto_ptr<_Up>&&)
shared_ptr(std::auto_ptr<_Tp1>&& __r);
^
/usr/include/c++/4.8/bits/shared_ptr.h:253:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:248:11: note: template<class _Tp1> std::shared_ptr<_Tp>::shared_ptr(const std::weak_ptr<_Tp1>&)
explicit shared_ptr(const weak_ptr<_Tp1>& __r)
^
/usr/include/c++/4.8/bits/shared_ptr.h:248:11: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:236:2: note: template<class _Tp1, class> std::shared_ptr<_Tp>::shared_ptr(std::shared_ptr<_Tp1>&&)
shared_ptr(shared_ptr<_Tp1>&& __r) noexcept
^
/usr/include/c++/4.8/bits/shared_ptr.h:236:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:226:7: note: std::shared_ptr<_Tp>::shared_ptr(std::shared_ptr<_Tp>&&) [with _Tp = void]
shared_ptr(shared_ptr&& __r) noexcept
^
/usr/include/c++/4.8/bits/shared_ptr.h:226:7: note: no known conversion for argument 1 from ‘<type error>’ to ‘std::shared_ptr<void>&&’
/usr/include/c++/4.8/bits/shared_ptr.h:218:2: note: template<class _Tp1, class> std::shared_ptr<_Tp>::shared_ptr(const std::shared_ptr<_Tp1>&)
shared_ptr(const shared_ptr<_Tp1>& __r) noexcept
^
/usr/include/c++/4.8/bits/shared_ptr.h:218:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:206:2: note: template<class _Tp1> std::shared_ptr<_Tp>::shared_ptr(const std::shared_ptr<_Tp1>&, _Tp*)
shared_ptr(const shared_ptr<_Tp1>& __r, _Tp* __p) noexcept
^
/usr/include/c++/4.8/bits/shared_ptr.h:206:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:184:2: note: template<class _Deleter, class _Alloc> std::shared_ptr<_Tp>::shared_ptr(std::nullptr_t, _Deleter, _Alloc)
shared_ptr(nullptr_t __p, _Deleter __d, _Alloc __a)
^
/usr/include/c++/4.8/bits/shared_ptr.h:184:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:165:2: note: template<class _Tp1, class _Deleter, class _Alloc> std::shared_ptr<_Tp>::shared_ptr(_Tp1*, _Deleter, _Alloc)
shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
^
/usr/include/c++/4.8/bits/shared_ptr.h:165:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:146:2: note: template<class _Deleter> std::shared_ptr<_Tp>::shared_ptr(std::nullptr_t, _Deleter)
shared_ptr(nullptr_t __p, _Deleter __d)
^
/usr/include/c++/4.8/bits/shared_ptr.h:146:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:129:2: note: template<class _Tp1, class _Deleter> std::shared_ptr<_Tp>::shared_ptr(_Tp1*, _Deleter)
shared_ptr(_Tp1* __p, _Deleter __d)
^
/usr/include/c++/4.8/bits/shared_ptr.h:129:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:112:11: note: template<class _Tp1> std::shared_ptr<_Tp>::shared_ptr(_Tp1*)
explicit shared_ptr(_Tp1* __p)
^
/usr/include/c++/4.8/bits/shared_ptr.h:112:11: note: template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/shared_ptr.h:103:7: note: std::shared_ptr<_Tp>::shared_ptr(const std::shared_ptr<_Tp>&) [with _Tp = void]
shared_ptr(const shared_ptr&) noexcept = default;
^
/usr/include/c++/4.8/bits/shared_ptr.h:103:7: note: no known conversion for argument 1 from ‘<type error>’ to ‘const std::shared_ptr<void>&’
/usr/include/c++/4.8/bits/shared_ptr.h:100:17: note: constexpr std::shared_ptr<_Tp>::shared_ptr() [with _Tp = void]
constexpr shared_ptr() noexcept
^
/usr/include/c++/4.8/bits/shared_ptr.h:100:17: note: candidate expects 0 arguments, 1 provided
make: *** [test.o] Error 1
However if I change new T {std::move(object)} to new T (std::move(object)). It works:
class Object {
std::shared_ptr<void> object_ptr;
public:
Object() {}
template<typename T>
Object(T&& object)
: object_ptr {new T (std::move(object)) } {}
virtual ~Object() {};
};
Why curly brace uniform initialization does not work here? Why is considered an initializater list in this case? Foo does not even have a constructor taking initializer list?
This was a known issue, and a defect in the standard (see CWG #1467). The proposal has been applied to the latest working paper (§8.5.4 [dcl.init.list]/3):
List-initialization of an object or reference of type T is defined as follows:
If T is a class type and the initializer list has a single element of type cv U, where U is T or a class derived
from T, the object is initialized from that element (by copy-initialization for copy-list-initialization, or
by direct-initialization for direct-list-initialization).
Note that trunk versions of Clang and GCC accept this code.
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