Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I do not understand why this compiles

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?

like image 323
Picaud Vincent Avatar asked Dec 03 '19 17:12

Picaud Vincent


People also ask

Why do we compile?

Because computer can't understand the source code directly. It will understand only object level code. Source codes are human readable format but the system cannot understand it. So, the compiler is intermediate between human readable format and machine-readable format.

How do you use compile in a sentence?

Example SentencesHe compiled a book of poems. She compiled a list of names. They took the best submissions and compiled them in a single issue of the magazine. We compiled our findings in the report.

What compiler compiles?

compiler, computer software that translates (compiles) source code written in a high-level language (e.g., C++) into a set of machine-language instructions that can be understood by a digital computer's CPU. Compilers are very large programs, with error-checking and other abilities.

What does it mean if a computer program compiles '?

Compile refers to the act of converting programs written in high level programming language, which is understandable and written by humans, into a low level binary language understood only by the computer.


2 Answers

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

like image 106
Brian Bi Avatar answered Oct 19 '22 11:10

Brian Bi


It's simply a function declaration declaring a to be a function returning A and taking one unnamed parameter of type B.

It is valid because function declarations as opposed to function definitions are allowed within function definitions.

like image 15
machine_1 Avatar answered Oct 19 '22 09:10

machine_1