简体   繁体   English

如何使用static_cast来解决重载函数?

[英]How to use static_cast to resolve overloaded functions?

  void hello()
  {
    cout << "helloworld" << endl;
  }

  void hello(string s)
  {
    cout << "hello " << s << endl;
  }

  void doWork()
  {
    thread t1(static_cast<void ()>(&hello));
    thread t2(static_cast<void (string)>(&hello),"bala");
    t1.join();
    t2.join();
  }

Error: 错误:

thread.cc|19 col 42| error: invalid static_cast from type '<unresolved overloaded function type>' to type 'void()'                                                          
thread.cc|20 col 48| error: invalid static_cast from type '<unresolved overloaded function type>' to type 'void(std::string) {aka void(std::basic_string<char>)}'

I know I can use typedef of function pointers or a lambda. 我知道我可以使用函数指针的typedef或lambda。 Isn't it possible to using static_cast ? 是不是可以使用static_cast

You must cast to function pointer types (not function types) 你必须强制转换为函数指针类型(不是函数类型)

thread t1(static_cast<void (*)()>(&hello));
                           ^^^

A function type (eg. void() ) is a type that denotes a function by its parameters and return types. 函数类型(例如, void() )是一种通过其参数和返回类型表示函数的类型。 However there can be no variables of these types in the program (except function themselves, these are lvalues of function types). 但是程序中不能有这些类型的变量(函数本身除外,这些是函数类型的左值)。 However, there can be references to functions or pointers to functions, of which you want to use the latter. 但是,可以引用函数或指向函数的指针,您要使用后者。

When you don't try to make variables (or temporary objects) of function type (eg. you typedef a function type, or use it as a template parameter), its use is OK. 当您尝试创建函数类型的变量(或临时对象)时(例如,您键入一个函数类型,或将其用作模板参数),它的使用就可以了。 std::function<void()> only uses the parameter to specify its parameters and return type, so its designers decided to use this sleek syntax. std::function<void()>仅使用参数来指定其参数和返回类型,因此其设计者决定使用这种流畅的语法。 Internally, it doesn't try to make variables with that type. 在内部,它不会尝试使用该类型创建变量。

The standard determines that when taking the address of an overloaded function the use of that address can be used to disambiguate. 该标准确定在获取重载函数的地址时,可以使用该地址来消除歧义。 That includes both assignment to a variable of the appropriate type or a cast. 这包括赋予适当类型的变量或转换。

What you are probably missing is that the type of &hello is not a function signature, but a function pointer, so the casts should be to void (*)() and/or void (*)(std::string) . 您可能缺少的是&hello的类型不是函数签名,而是函数指针,因此转换应该是void (*)()和/或void (*)(std::string)

void (*f)() = &hello;                  // target variable determines
                                       // the correct overload
thread thr( (void(*)())&hello );       // or a cast (C or static_cast<>)
thread thr( static_cast<void(*)()>(&hello) );

if you use std thread you can just write 如果你使用std线程,你可以写

std::thread(hello);
std::thread(hello, "blabla");

Why cast? 为什么演员? You can use std::bind or send the pointers directly 您可以使用std :: bind或直接发送指针

EDIT: 编辑:

Correct, this can NOT be done, a cast is definitely needed. 正确,这不可能完成,绝对需要演员阵容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM