1

I have returned to c++ after long years of Java/Node.js programming so, I am out of touch with the templates (I am working on that).

While revising concurrency, I accidentally stumbled upon the following case

class Test
{
};

int main()
{
  std::thread t(Test);
}

What I was experimenting with was, what if I pass a non-callable object and then I would study the error and source code. But I accidentally passed the type name. The program compiled an ran with no issue and I was baffled.

Then I tried something more stupid

int main()
{
  std::thread t1(int);
}

And somehow it worked as well. Though these two cases compiled and ran properly. I don't get how. If it tried t.join() or t1.join() I get following compilation error

t.join() cannot be resolved

I just simply just don't get what is happening in these two cases and how t.join() could not be resolved.

My research on it so far

I spend a significant amount of time trying to figure out how the compiler would have figured out whether the given value is callable. I came across this link: find out if a C++ object is callable It made a lot of sense because of member detection idiom but when I checked the source of gcc it seemed it used std::_Bind_simple of functional header/library to figure this out. But it didn't solve my query. Then, this post how std::thread constructor detects rvalue reference? asked the similar question but the answer made no sense to me.

Any sort of assistance will be very appreciated

Dr. Xperience
  • 475
  • 1
  • 5
  • 18

1 Answers1

3

Thanks to @Jaroda42 what I was doing was making a function declaration in local scope.

std::thread t(Test);

Where function being being t return type be std::thread and parameters being class type Test.

It was really stupid but this is what one get if one leaves a language for long time.

Dr. Xperience
  • 475
  • 1
  • 5
  • 18