C++14 – Effect of Auto on Compile Time

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?

Best Answer

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.

Related Question