3
  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. Isn't it possible to using static_cast?

P.W
  • 26,289
  • 6
  • 39
  • 76
balki
  • 26,394
  • 30
  • 105
  • 151

4 Answers4

11

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. 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. Internally, it doesn't try to make variables with that type.

jpalecek
  • 47,058
  • 7
  • 102
  • 144
  • That worked! But What is a function type? Eg. I don't need `*` when using `vector> allFuncs;` – balki Jan 11 '13 at 14:17
  • @balki: `std::function` takes the type of a function, but `&hello` is the address of one function. This is similar to `int` being the type of `a` and `int*` being the type of `&a`, except that the syntax for pointers to functions or arrays is a bit more convoluted and requires the extra set of parenthesis. – David Rodríguez - dribeas Jan 11 '13 at 14:20
  • Or function reference type. And `&` is unnecessary in either case. – Potatoswatter Jan 11 '13 at 14:22
1

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

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) );
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
0

if you use std thread you can just write

std::thread(hello);
std::thread(hello, "blabla");
mgr
  • 342
  • 2
  • 7
-1

Why cast? You can use std::bind or send the pointers directly

EDIT:

Correct, this can NOT be done, a cast is definitely needed.

jt234
  • 662
  • 6
  • 16
  • The cast is necessary to specify which overload to use. – Mike Seymour Jan 11 '13 at 14:16
  • 2
    This answer seems to have missed the point of the question. In the presence of overloads (in this case two `hello` overloads) the address of the name of the function in itself (`&hello`) is ambiguous and the language determines that the *use* of the function can be used by the compiler to disambiguate. – David Rodríguez - dribeas Jan 11 '13 at 14:16