I do not understand why this compiles

Picaud Vincent picture Picaud Vincent · Dec 3, 2019 · Viewed 7k times · Source

I'm certainly missing something, but I do not understand why this compiles (with both g++ & clang++):

struct A
{
};
struct B
{
};

int main()
{
  A a(B);
}

First of all, B is a type... not a value. How should I interpret this code?

Answer

Brian picture Brian · Dec 3, 2019

It's interpreted as the declaration of a function named a, which takes one argument of type B and returns A.