#include <iostream>
#include <regex>
int main(void)
{
std::cmatch cm;
std::regex_match("subject", cm, std::regex("(sub)(.*)"));
//std::for_each(cm.begin(), cm.end(), [](const std::sub_match<const char *> &s){ <---- Working statement
std::for_each(cm.begin(), cm.end(), [](const std::cmatch &s){ /*<--- Non-working statement*/
std::cout << "match:" << s.str() <<std::endl;
});
return 0;
}
The error is as following:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:853:9: error: no matching function for call to object of type '(lambda at main.cpp:73:41)'
__f(*__first);
^~~
main.cpp:73:10: note: in instantiation of function template specialization 'std::__1::for_each<std::__1::__wrap_iter<const std::__1::sub_match<const char *> *>, (lambda at main.cpp:73:41)>' requested here
std::for_each(cm.begin(), cm.end(), [](const std::match_results<const char*> &s){
^
main.cpp:73:41: note: candidate function not viable: no known conversion from 'const std::__1::sub_match<const char *>' to 'const std::match_results<const char *>' for 1st argument
std::for_each(cm.begin(), cm.end(), [](const std::match_results<const char*> &s){
^
maintool.cpp:73:41: note: conversion candidate of type 'void (*)(const std::match_results<const char *> &)'
1 error generated.
In non-working example why is template deduced as std::__1::for_each<std::__1::__wrap_iter<const std::__1::sub_match<const char *> *> ?
I was expecting param will be deduced to be std:::cmatch
Can you please explain how param deduction is working here?
std::cmatch is an alias for std::match_results<char const*>; you want std::sub_match<char const*>, whose alias is std::csub_match.
std::for_each(cm.begin(), cm.end(), [](const std::csub_match &s) { ... }
Related
I am trying to create a map between a particle and the location it is on. Essentially, I want my map M[loc] = p where loc is a std::vector <int> and p is a Particle.
This is the code that I have:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
class Particle{
public:
std::vector <int> coords; // the coordinates of the particles
std::string ptype;
int orientation;
bool operator<(const Particle& rhs)const{
return coords < rhs.coords;
}
// constructor
Particle (std::vector <int> crds, std::string type_, int orientation_): coords (crds), ptype (type_), orientation (orientation_){
}
// destructor
~Particle(){
}
Particle( const Particle &other); // copy constructor
// print location of the particle
void printCoords();
};
int main(int argc, char* argv[]){
std::map <std::vector <int>, Particle> OccupancyMap;
Particle p ({0,0,0}, "monomer", 0);
std::cout << p.ptype;
Particle pdash = p;
OccupancyMap[p.coords] = pdash ;
}
when I try to compile this code, I get a massive error message:
In file included from main.cpp:1:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/iostream:37:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ios:215:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale:14:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/string:506:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/string_view:175:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__string:57:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/algorithm:643:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/memory:677:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/tuple:1401:7: error: no matching constructor for initialization of 'Particle'
second(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/utility:507:11: note: in instantiation of function template specialization 'std::__1::pair<const std::__1::vector<int>, Particle>::pair<const std::__1::vector<int> &, 0>' requested here
: pair(__pc, __first_args, __second_args,
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/memory:1684:31: note: in instantiation of function template specialization 'std::__1::pair<const std::__1::vector<int>, Particle>::pair<const std::__1::vector<int> &>' requested here
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/memory:1562:21: note: in instantiation of function template specialization 'std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<std::__1::vector<int>, Particle>, void *>>::construct<std::__1::pair<const std::__1::vector<int>, Particle>, const std::__1::piecewise_construct_t &, std::__1::tuple<const std::__1::vector<int> &>, std::__1::tuple<>>' requested here
__a.construct(__p, _VSTD::forward<_Args>(__args)...);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/memory:1413:14: note: in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<std::__1::vector<int>, Particle>, void *>>>::__construct<std::__1::pair<const std::__1::vector<int>, Particle>, const std::__1::piecewise_construct_t &, std::__1::tuple<const std::__1::vector<int> &>, std::__1::tuple<>>' requested here
{__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__tree:2194:20: note: in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<std::__1::vector<int>, Particle>, void *>>>::construct<std::__1::pair<const std::__1::vector<int>, Particle>, const std::__1::piecewise_construct_t &, std::__1::tuple<const std::__1::vector<int> &>, std::__1::tuple<>>' requested here
__node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__tree:2139:29: note: in instantiation of function template specialization 'std::__1::__tree<std::__1::__value_type<std::__1::vector<int>, Particle>, std::__1::__map_value_compare<std::__1::vector<int>, std::__1::__value_type<std::__1::vector<int>, Particle>, std::__1::less<std::__1::vector<int>>, true>, std::__1::allocator<std::__1::__value_type<std::__1::vector<int>, Particle>>>::__construct_node<const std::__1::piecewise_construct_t &, std::__1::tuple<const std::__1::vector<int> &>, std::__1::tuple<>>' requested here
__node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/map:1521:20: note: in instantiation of function template specialization 'std::__1::__tree<std::__1::__value_type<std::__1::vector<int>, Particle>, std::__1::__map_value_compare<std::__1::vector<int>, std::__1::__value_type<std::__1::vector<int>, Particle>, std::__1::less<std::__1::vector<int>>, true>, std::__1::allocator<std::__1::__value_type<std::__1::vector<int>, Particle>>>::__emplace_unique_key_args<std::__1::vector<int>, const std::__1::piecewise_construct_t &, std::__1::tuple<const std::__1::vector<int> &>, std::__1::tuple<>>' requested here
return __tree_.__emplace_unique_key_args(__k,
^
main.cpp:51:15: note: in instantiation of member function 'std::__1::map<std::__1::vector<int>, Particle, std::__1::less<std::__1::vector<int>>, std::__1::allocator<std::__1::pair<const std::__1::vector<int>, Particle>>>::operator[]' requested here
OccupancyMap[p.coords] = pdash ;
^
main.cpp:35:5: note: candidate constructor not viable: requires single argument 'other', but no arguments were provided
Particle( const Particle &other); // copy constructor for some reason i dont know
^
main.cpp:26:5: note: candidate constructor not viable: requires 3 arguments, but 0 were provided
Particle (std::vector <int> crds, std::string type_, int orientation_): coords (crds), ptype (type_), orientation (orientation_){
^
1 error generated.
I don't quite understand what is going on here. What is the nature of the error here? What concept is it pointing towards?
I would appreciate any advice you have for me. I apologize if this is an obvious question.
My Compressor class has field QMap<QString, std::function<DataList(Compressor&, DataList &)> &> *techniques; and static method static DataList & sndStep(DataList &l);. Their type definitions:
typedef QPair<QPointF, QString> Data;
typedef QList<Data> DataList;
typedef QList<DataList> DataTable;
I'm trying to create a QMap which will contain the QString name of technique as Key and technique as Value.
At first, I tried to insert pair ("R0", sndStep){
Compressor::Compressor(const QString& s)
{
std::function<DataList(Compressor&, Datalist&)> f =
[] (DataList & l) {
return &Compressor::sndStep(DataList & l);
};
techniques->insert("R0", f);
}
But I got many errors. One of them is "no viable conversion":
compressor.cpp:8:53: error: no viable conversion from '(lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9)' to 'std::function<DataList (Compressor &, DataList &)>' (aka 'function<QList<QPair<QPointF, QString>> (Compressor &, QList<QPair<QPointF, QString>> &)>')
std_function.h:402:7: note: candidate constructor not viable: no known conversion from '(lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9)' to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
std_function.h:413:7: note: candidate constructor not viable: no known conversion from '(lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9)' to 'const std::function<QList<QPair<QPointF, QString>> (Compressor &, QList<QPair<QPointF, QString>> &)> &' for 1st argument
std_function.h:422:7: note: candidate constructor not viable: no known conversion from '(lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9)' to 'std::function<QList<QPair<QPointF, QString>> (Compressor &, QList<QPair<QPointF, QString>> &)> &&' for 1st argument
std_function.h:446:2: note: candidate template ignored: substitution failure [with _Functor = (lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9), $1 = void]: no type named 'type' in 'std::result_of<(lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9) &(Compressor &, QList<QPair<QPointF, QString>> &)>'
compressor.cpp:9:9: note: candidate function
How can I fix this error? Or maybe are there other ways to create a map of functions (not necessarily std::function)?
Here you are about to define f as a function taking two arguments (Compressor&, Datalist&) and returning DataList:
std::function<DataList(Compressor&, Datalist&)> f
but that's not the signature of the lambda:
[] (DataList & l) {
return &Compressor::sndStep(DataList & l);
};
which only takes one argument (DataList&) and returns something not known from the code snippet you've shown.
Here is the MCVE:
#include <iostream>
#include <regex>
std::string s()
{
return "test";
}
int main()
{
static const std::regex regex(R"(\w)");
std::smatch smatch;
if (std::regex_search(s(), smatch, regex)) {
std::cout << smatch[0] << std::endl;
}
return 0;
}
It compiles fine with:
$ clang++ -std=c++11 main.cpp
but not with:
$ clang++ -std=c++14 main.cpp
Error message in the later case (with -std=c++14):
main.cpp:14:9: error: call to deleted function 'regex_search'
if (std::regex_search(s(), smatch, regex)) {
^~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:5998:1: note:
candidate function [with _ST = std::__1::char_traits<char>, _SA = std::__1::allocator<char>,
_Ap = std::__1::allocator<std::__1::sub_match<std::__1::__wrap_iter<const char *> > >, _Cp =
char, _Tp = std::__1::regex_traits<char>] has been explicitly deleted
regex_search(const basic_string<_Cp, _ST, _SA>&& __s,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:2876:5: note:
candidate function [with _ST = std::__1::char_traits<char>, _SA = std::__1::allocator<char>,
_Ap = std::__1::allocator<std::__1::sub_match<std::__1::__wrap_iter<const char *> > >, _Cp =
char, _Tp = std::__1::regex_traits<char>]
regex_search(const basic_string<_Cp, _ST, _SA>& __s,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:2851:5: note:
candidate template ignored: deduced conflicting types for parameter '_Bp'
('std::__1::basic_string<char>' vs. 'std::__1::match_results<std::__1::__wrap_iter<const char
*>, std::__1::allocator<std::__1::sub_match<std::__1::__wrap_iter<const char *> > > >')
regex_search(_Bp, _Bp, const basic_regex<_Cp, _Tp>&,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:2857:5: note:
candidate template ignored: could not match 'const _Cp *' against 'std::string' (aka
'basic_string<char, char_traits<char>, allocator<char> >')
regex_search(const _Cp*, const _Cp*,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:2863:5: note:
candidate template ignored: could not match 'const _Cp *' against 'std::string' (aka
'basic_string<char, char_traits<char>, allocator<char> >')
regex_search(const _Cp*, match_results<const _Cp*, _Ap>&, const basic_regex<_Cp, _Tp>&,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:2869:5: note:
candidate template ignored: could not match 'basic_regex' against 'match_results'
regex_search(const basic_string<_Cp, _ST, _SA>& __s,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:5963:1: note:
candidate template ignored: could not match 'const _CharT *' against 'std::string' (aka
'basic_string<char, char_traits<char>, allocator<char> >')
regex_search(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:2839:5: note:
candidate function template not viable: requires at least 4 arguments, but 3 were provided
regex_search(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:2845:5: note:
candidate function template not viable: requires at least 4 arguments, but 3 were provided
regex_search(const _Cp*, const _Cp*, match_results<const _Cp*, _Ap>&,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:2884:5: note:
candidate function template not viable: requires at least 4 arguments, but 3 were provided
regex_search(__wrap_iter<_Iter> __first,
^
1 error generated.
Compiler version information:
$ clang++ -v
Apple LLVM version 7.0.0 (clang-700.0.72)
Target: x86_64-apple-darwin15.0.0
Thread model: posix
So, what's wrong?
There was a change going from C++11 to C++14 where std::regex_search is no longer allowed to take a r-value
template< class STraits, class SAlloc,
class Alloc, class CharT, class Traits >
bool regex_search( const std::basic_string<CharT,STraits,SAlloc>&&,
std::match_results<
typename std::basic_string<CharT,STraits,SAlloc>::const_iterator,
Alloc>&,
const std::basic_regex<CharT, Traits>&,
std::regex_constants::match_flag_type flags =
std::regex_constants::match_default ) = delete;
This was added as the overload that takes a const std::string&
is prohibited from accepting temporary strings, otherwise this function populates match_results m with string iterators that become invalid immediately.
So you can no longer pass a temporary to std::regex_search as of C++14
To fix your code we would simply store the return from s() into a variable in main and use that to call std::regex_search.
#include <iostream>
#include <regex>
std::string s()
{
return "test";
}
int main()
{
static const std::regex regex(R"(\w)");
std::smatch smatch;
auto search = s();
if (std::regex_search(search, smatch, regex)) {
std::cout << smatch[0] << std::endl;
}
return 0;
}
Live Example
This changed between C++11 and C++14. If we go to the cppreference section for std::regex_search we can see that overload that takes an rvalue reference was deleted since C++14:
template< class STraits, class SAlloc,
class Alloc, class CharT, class Traits > bool regex_search( const std::basic_string<CharT,STraits,SAlloc>&&,
std::match_results<
typename std::basic_string<CharT,STraits,SAlloc>::const_iterator,
Alloc
>&,
const std::basic_regex<CharT, Traits>&,
std::regex_constants::match_flag_type flags =
std::regex_constants::match_default ) = delete;
It was changed due to LWG issue 2329: regex_match()/regex_search() with match_results should forbid temporary strings which says (emphasis mine):
Consider the following code:
const regex r(R"(meow(\d+)\.txt)");
smatch m;
if (regex_match(dir_iter->path().filename().string(), m, r)) {
DoSomethingWith(m[1]);
}
This occasionally crashes. The problem is that
dir_iter->path().filename().string() returns a temporary string, so
the match_results contains invalidated iterators into a destroyed
temporary string.
It's fine for regex_match/regex_search(str, reg) to accept temporary
strings, because they just return bool. However, the overloads taking
match_results should forbid temporary strings.
and indeed if we use a non-temporary:
std::string s1 = s() ;
if (std::regex_search(s1, smatch, regex)) {
//...
}
it compiles (see it live) and no longer exhibits undefined behavior.
Interesting to note that gcc/libstdc++ has this overload deleted in C++11 mode as well see it live. Since this is undefined behavior it seems like a good solution.
This issue also pops up in other areas of the library see Visual Studio regex_iterator Bug? which deals with the same issue but with regex_iterator/regex_token_iterator.
This not a bug, but expected behaviour.
The reason is that s() returns a temporary string, regex_search makes use of regex_match and consequently if a temporary string was utilized match results would contain iterators to a string that no longer exists. This would have been undefined behaviour. Thus, the committee abolished this regex_search overload in C++14.
You can also confirm in the standard 28.4 Header synopsis [re.syn]:
template <class ST, class SA, class Allocator, class charT, class traits>
bool regex_search(const basic_string<charT, ST, SA>&&,
match_results<
typename basic_string<charT, ST, SA>::const_iterator,
Allocator>&,
const basic_regex<charT, traits>&,
regex_constants::match_flag_type =
regex_constants::match_default) = delete;
As you can see the overload that takes a rvalue to a basic_string is marked deleted.
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
I define the operator<< for std::string objects:
std::string & operator<< (std::string & left, std::string & right){
return left += right;
}
Then I use it:
std::string t1("t1"), t2("t2"), t3;
t3 = t2 << t1;
And get from a compiler:
t.cpp: In function 'int main()':
t.cpp:44:28: error: no matching function for call to 'operator<<(std::string&, std::string&)'
t.cpp:44:28: note: candidates are:
In file included from d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/iostream:40:0,
from connection.h:10,
from login.cpp:1:
d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:600:5: note: template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT
, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)
d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:600:5: note: template argument deduction/substitution failed:
t.cpp:44:28: note: 'std::string {aka std::basic_string<char>}' is not derived from 'std::basic_ostream<_CharT, _Traits>'
In file included from d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/iostream:40:0,
Why talks it about ostream and does not talk about string? I.e. why it does not take in account my definition of the operator<< ?
Thanks.
Update. For those who is able just to say "why do you create operator<< for strings?" and is not able to say any helpful things:
std::string & operator<< (std::string & left, const int num){
return left += std::to_string(num);
}
std::string t3;
t3 << 3 << 5;
std::cout << t3 << std::endl;
And log:
t.cpp: In function 'int main()':
t.cpp:45:12: error: no match for 'operator<<' in 't3 << 3'
t.cpp:45:12: note: candidates are:
In file included from d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/iostream:40:0,
from connection.h:10,
from login.cpp:1:
d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:600:5: note: template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT
, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)
d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:600:5: note: template argument deduction/substitution failed:
t.cpp:45:12: note: 'std::string {aka std::basic_string<char>}' is not derived from 'std::basic_ostream<_CharT, _Traits>'
It does work:
#include <iostream>
#include <string>
std::string & operator<< (std::string & left, std::string & right){
return left += right;
}
int main()
{
std::string t1("t1"), t2("t2"), t3;
t3 = t2 << t1;
std::cout << t3;
}
Output: t2t1 [GCC 4.8.1]
The compiler output, as you say yourself, indicates that your operator overload is not even visible. You must not have your declaration in the right place.
This is not really a good idea, anyway: you will simply confuse the heck out of anybody who reads your code. Strings are not streams.