I need a type variable std::function<void(char**)>. Here is a simple example of giving up on this.
What I'm trying to understand is the following:
- What does auto do that the compiler does not complain when I call
jj_2a(5, 6)? This function has all the parameters associated with it. - But if I do not use auto, I get the behavior that I expect (compilation with arguments). Thus, it is clear that it
function<void(void)>’s not at all what the car decided. - If I bind the first argument, not the second (
jj_3), then a call with two arguments works (but does not match the wrong argument, according to my mental model) when called with one argument (which, I think, should work) does not compile. - Using
std::functionalfor jj_3_fsays "no viable conversion", although the error message does not help me yet.
See below for compiler and specific errors. This is linux, clang 3.8.0, ubuntu 16.04.1.
#include <functional>
#include <iostream>
void jj_1(int x, int y) { std::cout << x << ' ' << y << std::endl; }
int main() {
using namespace std::placeholders;
auto jj_2a = std::bind(jj_1, 3, 2);
jj_2a(5, 6);
jj_2a();
std::function<void(void)> jj_2a_f = std::bind(jj_1, 30, 20);
jj_2a_f();
auto jj_2b = std::bind(jj_1, _2, _1);
jj_2b(5, 6);
auto jj_3 = std::bind(jj_1, 3, _2);
jj_3(5, 6);
}
I will compile this with
clang++ -std=c++14 -Wall -Wextra /tmp/foo.cc -o /tmp/foo
Compiler related warnings are jj_3(7)as follows:
/tmp/foo.cc:21:7: error: no matching function for call to object of type 'std::_Bind<void (*(int,
std::_Placeholder<2>))(int, int)>'
jj_3(7); // Compile error!
^~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/functional:1129:2: note:
candidate template ignored: substitution failure [with _Args = <int>]: no viable conversion from
'std::_No_tuple_element' to 'int'
operator()(_Args&&... __args)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/functional:1143:2: note:
candidate template ignored: substitution failure [with _Args = <int>]: no viable conversion from
'std::_No_tuple_element' to 'int'
operator()(_Args&&... __args) const
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/functional:1157:2: note:
candidate template ignored: substitution failure [with _Args = <int>]: no viable conversion from
'std::_No_tuple_element' to 'int'
operator()(_Args&&... __args) volatile
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/functional:1171:2: note:
candidate template ignored: substitution failure [with _Args = <int>]: no viable conversion from
'std::_No_tuple_element' to 'int'
operator()(_Args&&... __args) const volatile
^
1 error generated.
Compiler related warnings are jj_3_fas follows:
/tmp/foo.cc:23:32: error: no viable conversion from 'typename _Bind_helper<__is_socketlike<void (&)(int,
int)>::value, void (&)(int, int), int, const _Placeholder<2> &>::type' (aka '_Bind<void (*(int,
std::_Placeholder<2>))(int, int)>') to 'std::function<void (int)>'
std::function<void(int)> jj_3_f = std::bind(jj_1, 3, _2); // Compile error, no viable conversion!
^ ~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/functional:2008:7: note:
candidate constructor not viable: no known conversion from 'typename
_Bind_helper<__is_socketlike<void (&)(int, int)>::value, void (&)(int, int), int, const
_Placeholder<2> &>::type' (aka '_Bind<void (*(int, std::_Placeholder<2>))(int, int)>') to
'nullptr_t' for 1st argument
function(nullptr_t) noexcept
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/functional:2019:7: note:
candidate constructor not viable: no known conversion from 'typename
_Bind_helper<__is_socketlike<void (&)(int, int)>::value, void (&)(int, int), int, const
_Placeholder<2> &>::type' (aka '_Bind<void (*(int, std::_Placeholder<2>))(int, int)>') to 'const
std::function<void (int)> &' for 1st argument
function(const function& __x);
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/functional:2028:7: note:
candidate constructor not viable: no known conversion from 'typename
_Bind_helper<__is_socketlike<void (&)(int, int)>::value, void (&)(int, int), int, const
_Placeholder<2> &>::type' (aka '_Bind<void (*(int, std::_Placeholder<2>))(int, int)>') to
'std::function<void (int)> &&' for 1st argument
function(function&& __x) : _Function_base()
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/functional:2054:2: note:
candidate template ignored: substitution failure [with _Functor = std::_Bind<void (*(int,
std::_Placeholder<2>))(int, int)>, $1 = void]: no type named 'type' in
'std::result_of<std::_Bind<void (*(int, std::_Placeholder<2>))(int, int)> (int)>'
function(_Functor);
^
1 error generated.
Fwiw, what I really want to do is parallel. I have a function
void MyFunction(A& a, B& b, const char** thing);
where Aand Bare class names. I have another function that expects a callback this way:
C DoStuff(const std::string& s, std::function<void(const char** thing)> f);
which then i try to call
DoStuff("Hello!", std::bind(MyFunction, an_a, a_b, _3));
and I get errors regarding the lack of a viable transformation.