Does Auto Deduce the Type at Compile Time or Runtime in C++ 11

Does auto deduce the type at compile time or runtime in C++ 11?

Compile time. In C++, runtime type information is stripped during compilation(without RTTI or virtual inheritance). It is not possible, in fact, to inspect the primitive type at runtime.

Does auto deduce types in compile time or run time in C++11?

auto works at compile time.

You are correct that the return type of give_something can't be deduced at compile-time. In such a case, your code will not compile. Clang gives this error message for your example:

main.cpp:8:5: error: 'auto' in return type deduced as 'float' here but deduced 
as 'std::__cxx11::basic_string<char>' in earlier return
statement
return 6.66f;

Does auto in C++ 11 make compile time longer?

auto is one character longer than int, so the lexer definitely has to do more work.

On the other hand, the compiler no longer has to check that the user provided an appropriate type, so my best guess is that auto will be slightly faster.

In the end, you should probably not decide between type inference and explicit typing based on performance considerations. Intent and clarity should be the deciding factors.

How can auto distinguish short from long at compile time?

The compiler works with the information present which in your case is the integer literal 12. So it deduces the foo to be of type int. It does not anticipate anything. You can use the appropriate integer literal suffix:

auto foo = 12ul;

to force the foo to be deduced as unsigned long. You can't define the variable to be of type int and then down the line expect the compiler to somehow change it into another type just because you assigned a different value that will not fit into previously used type. If you did that it would simply result in integer overflow which is undefined behavior.

For more info on the subject check out the auto specifier and auto type deduction reference.

Is `auto` specifier slower in compilation time?

auto is asking the C++11 compiler to make some limited kind of type inference (look into Ocaml if you want some more sexy type inference language). But the overhead is compile-time only.

If you replace auto a=1+2; with int a=1+2; (both have the same meaning, see answer by simplicis) and if you ask your compiler to optimize (and probably even without asking for optimizations) you'll probably get the same machine code. See also this.

If using GCC try to compile a small C++11 foo.cc file with g++ -Wall -fverbose-asm -O -S foo.cc and look (with an editor) into the generated foo.s assembler file. You'll see no difference in the generated code (but the assembler file might perhaps change slightly, e.g. because of metadata like debug information etc.)

If you are concerned about slower compile-time I guess that using auto is not a decisive factor (probably, overloading could be more costly in compilation time). C++11 is nearly designed to practically require a lot of optimizations (in particular sophisticated inlining and constant folding and dead code elimination), and its "parsing" (notably header inclusion and template expansion) is costly.

Precompiling headers and parallel builds with make -j (and perhaps ccache or distcc) might help in improving the overall compilation time, much more than avoiding auto.

And if you wanted to systematically avoid auto (in particular in range-for loops like std::map<std::string,int> dict; for (auto it: dict) {...}) you'll end up typing much more source code (whose parsing and checking takes significant time) with more risks of error. As explained here, you might guess slightly wrongly the type, and expliciting it (slightly wrongly) might slow down the execution of your code because of additional conversions.

If using GCC you might pass the -ftime-report to g++ and get time measurements about various GCC passes and phases.

Retrieving the type of auto in C++11 without executing the program

It is going to be a PITA, but you can declare an incomplete struct template accepting a single type parameter.

Given the variable x you want to know the type of, you can use the struct with decltype(x) and that will lead to a compiler error that will show you the inferred type.

For example:

template<class Type> struct S;

int main() {
auto x = ...;
S<decltype(x)>();
}

Live demo

which will produce an error message in the form:

error: implicit instantiation of undefined template 'S<X>' (clang++)
error: invalid use of incomplete type 'struct S<X>' (g++)

with X being the inferred type. In this particular case the type is int.

Trivia: This has been recommended by Scott Meyer in one of the recent NDC 2014's videos (I don't remember which one).



Related Topics



Leave a reply



Submit