Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using 'auto' type deduction - how to find out what type the compiler deduced?

How can I find out what type the compiler deduced when using the auto keyword?

Example 1: Simpler

auto tickTime = 0.001; 

Was this deduced as a float or a double?

Example 2: More complex (and my present headache):

typedef std::ratio<1, 1> sec; std::chrono::duration<double, sec > timePerTick2{0.001};  auto nextTickTime = std::chrono::high_resolution_clock::now() + timePerTick2; 

What type is nextTickTime?

The problem I'm having is when I try to send nextTickTime to std::cout. I get the following error:

./main.cpp: In function ‘int main(int, char**)’: ./main.cpp:143:16: error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’   std::cout << std::setprecision(12) << nextTickTime << std::endl; // time in seconds             ^ In file included from /usr/include/c++/4.8.2/iostream:39:0,              from ./main.cpp:10: /usr/include/c++/4.8.2/ostream:602:5: error:   initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<double, std::ratio<1l, 1000000000l> > >]’  operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x) 
like image 595
kmiklas Avatar asked Aug 08 '16 02:08

kmiklas


People also ask

What is automatic type deduction?

With auto type deduction enabled, you no longer need to specify a type while declaring a variable. Instead, the compiler deduces the type of an auto variable from the type of its initializer expression.

What data type is auto?

As explained above, the auto keyword in C++ detects the data type of a variable by itself. This means that we can replace the data type of a variable with the keyword auto in C++. The compiler will automatically detect the variable's data type at compile time.

What is Decltype used for in C++?

The decltype type specifier yields the type of a specified expression. The decltype type specifier, together with the auto keyword, is useful primarily to developers who write template libraries. Use auto and decltype to declare a template function whose return type depends on the types of its template arguments.

Which keyword can be used in C ++ 11 to denote that the compiler has to deduce the type of variable on the fly?

1) auto keyword: The auto keyword specifies that the type of the variable that is being declared will be automatically deducted from its initializer.


2 Answers

I like to use idea from Effective Modern C++ which uses non-implemented template; the type is output with compiler error:

 template<typename T> struct TD; 

Now for auto variable var, after its definition add:

 TD<decltype(var)> td; 

And watch error message for your compiler, it will contain type of var.

like image 72
marcinj Avatar answered Sep 23 '22 02:09

marcinj


A lo-fi trick that doesn't require any prior helper definitions is:

typename decltype(nextTickTime)::_ 

The compiler will complain that _ isn't a member of whatever type nextTickTime is.

like image 44
John McFarlane Avatar answered Sep 21 '22 02:09

John McFarlane