2

Let's say, i want to this thing:

    std::thread(std::sin,2.9);

But i get unresolved function type error. How can i resolve it? std::sin is overloaded, not template (why??)

1

4 Answers 4

4

Depending on what headers you included, there are the following:

template<class T> complex<T>  sin (const complex<T>&);  // <complex>
template<class T> valarray<T> sin (const valarray<T>&); // <valarray>
float sin(float);             // <cmath>
long double sin(long double); // <cmath>

As you can see, there are templates involved, though that's actually not strictly relevant here.

You simply need to inform the compiler which [potential] overload you want (probably just out of the latter two):

std::thread((float(*)(float))&std::sin, 2.9);
Sign up to request clarification or add additional context in comments.

4 Comments

If you wish to avoid C-cast, the appropriate C++ cast is static_cast.
@MatthieuM.: I don't particularly (it's completely superfluous here; what am I going to do, accidentally cast away constness?), but thanks.
I prefer C++ casts because it's more obvious a cast is going on, subjective, I know :)
@MatthieuM.: I prefer not abusing commas!
2

You could

  typedef double (*sind)(double);

  std::thread((sind) std::sin,2.9);

Comments

1

You have to cast the function pointer to the type of the right overload:

std::thread((float(*)(float))std::sin, 2.9);

Comments

1

I prefer the lambda solution. It reads much cleaner than anything involving a function pointer. It will also be faster than using a function pointer, but that doesn't usually matter.

std::thread( []( double x ) { return std::sin(x); }, 2.9 );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.