Why does my std::function have no viable conversion? - c++

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.

Related

No viable overloaded '=' at std::vector

I have this c++ program with the following types:
typedef pcl::PointXYZI PointType;
typedef pcl::PointCloud<PointType> PointCloudType;
typedef std::vector<PointCloudType::Ptr> CloudPtrList;
and this in main:
CloudPtrList clusterPtr(2);
for (int i = 0; i < clusterPtr.size(); ++i)
{
clusterPtr[i] = boost::make_shared<PointCloudType>();
}
And i keep getting this error ' no viable overloaded =' when i try to initialize this vector (the for above)
clusterPtr[i] = boost::make_shared<PointCloudType>();
This is the full error:
No viable overloaded '='
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3737:17: Candidate function not viable: no known conversion from 'typename boost::detail::sp_if_not_array<PointCloud<PointXYZI> >::type' (aka 'shared_ptr<pcl::PointCloud<pcl::PointXYZI> >') to 'const std::__1::shared_ptr<pcl::PointCloud<pcl::PointXYZI> >' for 1st argument
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3748:17: Candidate function not viable: no known conversion from 'typename boost::detail::sp_if_not_array<PointCloud<PointXYZI> >::type' (aka 'shared_ptr<pcl::PointCloud<pcl::PointXYZI> >') to 'std::__1::shared_ptr<pcl::PointCloud<pcl::PointXYZI> >' for 1st argument
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3745:9: Candidate template ignored: could not match 'std::__1::shared_ptr' against 'boost::shared_ptr'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3756:9: Candidate template ignored: could not match 'std::__1::shared_ptr' against 'boost::shared_ptr'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3766:9: Candidate template ignored: could not match 'auto_ptr' against 'shared_ptr'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3790:9: Candidate template ignored: could not match 'unique_ptr' against 'shared_ptr'
As can be seen in the error message ...
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3737:17: Candidate function not viable: no known conversion from 'typename boost::detail::sp_if_not_array<PointCloud<PointXYZI> >::type' (aka 'shared_ptr<pcl::PointCloud<pcl::PointXYZI> >') to 'const std::__1::shared_ptr<pcl::PointCloud<pcl::PointXYZI> >' for 1st argument
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:3748:17: Candidate function not viable: no known conversion from 'typename boost::detail::sp_if_not_array<PointCloud<PointXYZI> >::type' (aka 'shared_ptr<pcl::PointCloud<pcl::PointXYZI> >') to 'std::__1::shared_ptr<pcl::PointCloud<pcl::PointXYZI> >' for 1st argument
...you are using a newer version of PCL in which boost::shared_ptr has been replaced by std::shared_ptr:
#include <pcl/common/projection_matrix.h>
#include <pcl/point_types.h>
//#include <boost/make_shared.hpp> // boost::make_shared
#include <memory> // std::make_shared
typedef pcl::PointCloud<pcl::PointXYZI> PointCloudType;
int main() {
std::vector<PointCloudType::Ptr> clusterPtr(2);
for(int i = 0; i < clusterPtr.size(); ++i) {
clusterPtr[i] = std::make_shared<PointCloudType>(); // std::make_shared
}
}

Cannot convert std::reference<T> to std::reference<const T> on Clang

I would expect that std::reference_wrapper would work as a reference in terms of converting non-const into const, like:
int a = 10;
int& refA = a;
const int& constRefA = refA;
The following code compiles and works fine in MSVC and GCC, but not on Clang. I just don't understand why, is it UB, or actually an issue on Clang compiler?
#include <functional>
#include <optional>
int main()
{
int a = 10;
std::reference_wrapper<int> ref = a;
std::reference_wrapper<const int> constRef = ref;
std::optional<std::reference_wrapper<int>> optRef = a;
std::optional<std::reference_wrapper<const int>> optConstRef = optRef;
return 0;
}
On Clang only, displays the following error:
prog.cc:13:39: error: no viable conversion from 'reference_wrapper<int>' to 'reference_wrapper<const int>'
std::reference_wrapper<const int> constRef = ref;
https://wandbox.org/permlink/FSY4tCvE9B17hbVn
prog.cc:13:39: error: no viable conversion from 'reference_wrapper<int>' to 'reference_wrapper<const int>'
std::reference_wrapper<const int> constRef = ref;
^ ~~~
/opt/wandbox/clang-head/include/c++/v1/__functional_base:374:28: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'std::reference_wrapper<int>' to 'const std::reference_wrapper<const int> &' for 1st argument
class _LIBCPP_TEMPLATE_VIS reference_wrapper
^
/opt/wandbox/clang-head/include/c++/v1/__functional_base:374:28: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'std::reference_wrapper<int>' to 'std::reference_wrapper<const int> &&' for 1st argument
/opt/wandbox/clang-head/include/c++/v1/__functional_base:386:5: note: candidate constructor not viable: no known conversion from 'std::reference_wrapper<int>' to 'std::reference_wrapper<const int>::type &' (aka 'const int &') for 1st argument
reference_wrapper(type& __f) _NOEXCEPT
^
/opt/wandbox/clang-head/include/c++/v1/__functional_base:389:14: note: candidate constructor not viable: no known conversion from 'std::reference_wrapper<int>' to 'std::reference_wrapper<const int>::type &&' (aka 'const int &&') for 1st argument
private: reference_wrapper(type&&); public: // = delete; // do not bind to temps
^
/opt/wandbox/clang-head/include/c++/v1/__functional_base:394:5: note: candidate function
operator type&() const _NOEXCEPT {return *__f_;}
^
prog.cc:16:54: error: no viable conversion from 'optional<reference_wrapper<int>>' to 'optional<reference_wrapper<const int>>'
std::optional<std::reference_wrapper<const int>> optConstRef = optRef;
^ ~~~~~~
/opt/wandbox/clang-head/include/c++/v1/optional:689:41: note: candidate constructor not viable: no known conversion from 'std::optional<std::reference_wrapper<int>>' to 'const std::optional<std::reference_wrapper<const int>> &' for 1st argument
_LIBCPP_INLINE_VISIBILITY constexpr optional(const optional&) = default;
^
/opt/wandbox/clang-head/include/c++/v1/optional:690:41: note: candidate constructor not viable: no known conversion from 'std::optional<std::reference_wrapper<int>>' to 'std::optional<std::reference_wrapper<const int>> &&' for 1st argument
_LIBCPP_INLINE_VISIBILITY constexpr optional(optional&&) = default;
^
/opt/wandbox/clang-head/include/c++/v1/optional:691:41: note: candidate constructor not viable: no known conversion from 'std::optional<std::reference_wrapper<int>>' to 'std::nullopt_t' for 1st argument
_LIBCPP_INLINE_VISIBILITY constexpr optional(nullopt_t) noexcept {}
^
/opt/wandbox/clang-head/include/c++/v1/optional:715:15: note: candidate template ignored: substitution failure [with _Up = std::optional<std::reference_wrapper<int>> &]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
constexpr optional(_Up&& __v)
^
/opt/wandbox/clang-head/include/c++/v1/optional:730:5: note: candidate template ignored: substitution failure [with _Up = std::reference_wrapper<int>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
optional(const optional<_Up>& __v)
^
/opt/wandbox/clang-head/include/c++/v1/optional:748:5: note: candidate template ignored: substitution failure [with _Up = std::reference_wrapper<int>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
optional(optional<_Up>&& __v)
^
/opt/wandbox/clang-head/include/c++/v1/optional:701:24: note: explicit constructor is not a candidate
constexpr explicit optional(_InPlaceT, _Args&&... __args)
^
/opt/wandbox/clang-head/include/c++/v1/optional:722:24: note: explicit constructor is not a candidate
constexpr explicit optional(_Up&& __v)
^
/opt/wandbox/clang-head/include/c++/v1/optional:738:14: note: explicit constructor is not a candidate
explicit optional(const optional<_Up>& __v)
^
/opt/wandbox/clang-head/include/c++/v1/optional:756:14: note: explicit constructor is not a candidate
explicit optional(optional<_Up>&& __v)
^
2 errors generated.
The std library you are using on wandbox has a bug. It only found 2 constructors and a conversion operator.
Compilers and standard libraries are not always in sync.
printf("Hello World");
int a = 10;
std::reference_wrapper<int> ref = a;
std::reference_wrapper<const int> constRef( ref );
std::optional<std::reference_wrapper<int>> optRef = a;
std::optional<std::reference_wrapper<const int>> optConstRef( optRef );
return 0;
By making the conversions explicit it works. I don't know why; there aren't any explicit conversions differences in my reading of reference_wrapper construction and conversion operators.
But the lack of implicit reference wrapping conversion would explain why the optional would want it.
In any case, this is clearly a bug. The universal conversion constructor for reference_wrapper<const int> should apply if:
void FUN(int const&) {}
FUN(ref)
overload resolution works, and it does for a reference_wrapper<int>.

Is this c++ template param deduction incorrect?

#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) { ... }

"invalid operands to binary expression" when using custom struct as index of map in C++

The code:
struct Tag {
std::string left_tag, right_tag;
};
When I try to use this->_tags[__tag] = true;, I got the error:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__functional_base:63:21: error: invalid operands to binary expression ('const Tag' and 'const Tag')
{return __x < __y;}
~~~ ^ ~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/map:1207:17: note: in instantiation of member function 'std::__1::less<Tag>::operator()' requested here
if (__tree_.value_comp().key_comp()(__k, __nd->__value_.__cc.first))
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/map:1376:36: note: in instantiation of member function 'std::__1::map<Tag, bool, std::__1::less<Tag>, std::__1::allocator<std::__1::pair<const Tag, bool> > >::__find_equal_key' requested here
__node_base_pointer& __child = __find_equal_key(__parent, __k);
^
/Users/xxx/GitHubWorking/MarkupUtils/Syntax.h:69:20: note: in instantiation of member function 'std::__1::map<Tag, bool, std::__1::less<Tag>, std::__1::allocator<std::__1::pair<const Tag, bool> > >::operator[]' requested here
this->_tags[__tag] = true;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:419:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Tag'
operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
^
1 error generated.
I am stunned by the series of long stl errors.
According to the most deep error, it is caused by use of const Tag, but there does not seem to be any in my code.
You need to define an operator< for example
struct Tag
{
std::string left_tag, right_tag;
bool operator< (const Tag& rhs) const
{
return this->left_tag < rhs.left_tag ||
(this->left_tag == rhs.left_tag && this->right_tag < rhs.right_tag);
}
};
This will define the less-than operator to sort two Tag instances. As written in the above example, this will sort by preferring left_tag, then right_tag.

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