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?
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.
This is a compile-type specifier, it does not affect the running time.
The auto keyword directs the compiler to use the initialization expression of a declared variable, or lambda expression parameter, to deduce its type.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With