c++ - std::bind 分配给 std::function

标签 c++ templates c++11 c++14

我想要一个 std::function<void(char**)> 类型的变量.这是未能做到这一点的简单示例。

我想了解的是:

  • 当我调用 jj_2a(5, 6) 时,编译器不会提示什么是 auto ?该函数绑定(bind)了所有参数。
  • 但如果我不使用 auto,我会得到我期望的行为(带参数的编译错误)。这么清楚function<void(void)>根本不是自动决定的。
  • 如果我绑定(bind)第一个参数而不是第二个参数 ( jj_3 ),那么使用两个参数调用会起作用(但根据我的心智模型,会丢弃错误的参数)而使用一个参数调用(我认为应该起作用)不编译。
  • 使用 std::functional对于 jj_3_f说“没有可行的转换”,尽管到目前为止错误消息对我没有帮助。

有关编译器和特定错误,请参见下文。这是 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; // for _1, _2, _3...

  auto jj_2a = std::bind(jj_1, 3, 2);
  jj_2a(5, 6);  // This works, prints "3 2", no compiler warning, is auto drunk?
  jj_2a();      // This also works, prints "3 2".
  std::function<void(void)> jj_2a_f = std::bind(jj_1, 30, 20);
  //jj_2a_f(50, 60);  // Compile error, good!
  jj_2a_f();  // This works, prints "30 20", good!

  auto jj_2b = std::bind(jj_1, _2, _1);
  jj_2b(5, 6);  // This works, prints "6 5", good.

  auto jj_3 = std::bind(jj_1, 3, _2);
  jj_3(5, 6);  // This works, prints "3 6", so it's the first arg that is dropped!
  //jj_3(7);     // Compile error!

  //std::function<void(int)> jj_3_f = std::bind(jj_1, 3, _2);  // Compile error, no viable conversion!
  //jj_4(11);
}

我编译这个

clang++  -std=c++14 -Wall -Wextra /tmp/foo.cc -o /tmp/foo

jj_3(7) 相关的编译器警告这些是:

/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.

jj_3_f 相关的编译器警告这些是:

/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,我真正想做的事情是平行的。我有一个功能

void MyFunction(A& a, B& b, const char** thing);

哪里AB是类名。我有另一个函数需要这样的回调:

C DoStuff(const std::string& s, std::function<void(const char** thing)> f);

然后我尝试调用它

DoStuff("Hello!", std::bind(MyFunction, an_a, a_b, _3));

我收到关于没有可行转换的错误。

最佳答案

注意:此答案假定您已阅读 Why do objects returned from bind ignore extra arguments?

std::bind 返回的对象不是std::function;它是一个未指定类型的对象,在许多方面与 std::function 不同;对于您的情况最重要的是,它会忽略传递给它的任何额外参数。

如果您在绑定(bind)表达式中使用最多 _2 的占位符(例如),则可以使用任意数量的参数调用返回的对象,只要该数量至少为 2。

  • What is auto doing that the compiler doesn't complain when I call jj_2a(5, 6)? That function has all its parameters bound.

您正在传递额外的参数;这些额外的参数将被忽略。

  • But if I don't use auto, I get the behaviour I expect (compile error with arguments). So clearly function is not at all what auto decided.

正确; bind 不返回 std::function,它返回一个未指定类型的可调用对象,可以(取决于签名)用于构造 std::函数

  • If I bind the first argument and not the second (jj_3), then calling with two arguments works (but drops the wrong argument, according to my mental model) while calling with one argument (which I think should work) doesn't compile.

如果您使用 std::placeholders::_2,您必须至少传递 2 个参数。 std::placeholders::_2 选择传递给从 bind 返回的对象的第二个参数。

  • Using std::functional for jj_3_f says "no viable conversion", though the error message isn't so far helping me.

如果你使用std::placeholders::_2,你必须传递至少2个参数; std::function 的构造函数检查这个。

关于c++ - std::bind 分配给 std::function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38893386/

相关文章:

c++ - 模板特化适用于 int 而失败于 double 作为一个类

c++ - 在链接列表中的第 n 个位置插入显示段错误

c++ - 两个头文件可以包含 C++ 中命名相同的类吗?

c++ - 根据 C++11 中的模板参数选择数组大小?

c++ - 为不同命名空间中的类专门化模板函数

c++ - MSVC2013 和 MingW 之间的差异导致最令人烦恼的解析问题

c++ - 从 abs(double) 而不是 int 获得双重结果

java - 更改 Java Struts 应用程序中的默认文件结构

c++ - 这个 C++ 模板中的迭代器类型应该是什么?

C++0x "Hello Concurrent World"在 g++/linux 上立即出现段错误?