Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The effect of auto on compile time

Tags:

c++

c++14

The new auto keyword that we got in C++11 looks quite templat'ish to me so my question is - will it incur the same compile time bloat as templates do?

The same question in regards to polymorphic lambdas:

 [](auto val) {…}

this is essentially a template lambda - will this impact compile time or not?

like image 272
Zeks Avatar asked Jul 07 '13 00:07

Zeks


People also ask

Does Auto increase compile time?

It is used before the name of the variable or the function to infer the return type of both of them. When the auto keyword is used, it is evaluated at the compilation time. Since the compiler has to do this, it increases the compilation time to a few seconds, which is almost negligible.

Does C++ auto take more time?

This is a compile-type specifier, it does not affect the running time.

What does C++ auto do?

The auto keyword directs the compiler to use the initialization expression of a declared variable, or lambda expression parameter, to deduce its type.


1 Answers

The auto keyword of C++11 is far less heavyweight than templates - its compile-time "overhead" is comparable to that of sizeof, which means it's close to zero.

Unlike templates where the compiler needs to perform sizeable amount of computation during the expansion (the template language in C++ is Turing-complete), the auto keyword requires the compiler to figure out the type of an expression, which is something the compiler knows anyway. In fact, it would have to figure out the type of the expression even without the auto keyword to decide if type conversions need to be applied.

like image 180
Sergey Kalinichenko Avatar answered Sep 18 '22 12:09

Sergey Kalinichenko