如何找出编译器在使用auto
关键字时推断出的类型?
示例 1:更简单
auto tickTime = 0.001;
这是推论为float
还是double?
示例2:更复杂(以及我现在的头痛(:
typedef std::ratio<1, 1> sec;
std::chrono::duration<double, sec > timePerTick2{0.001};
auto nextTickTime = std::chrono::high_resolution_clock::now() + timePerTick2;
nextTickTime
是什么类型?
我遇到的问题是当我尝试将nextTickTime
发送到std::cout
时。我收到以下错误:
./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)
我喜欢使用有效现代C++的想法,它使用未实现的模板;该类型输出并带有编译器错误:
template<typename T> struct TD;
现在对于自动变量var
,在其定义后添加:
TD<decltype(var)> td;
并注意编译器的错误消息,它将包含var
的类型。
不需要任何先前帮助程序定义的低保真技巧是:
typename decltype(nextTickTime)::_
编译器会抱怨_
不是nextTickTime
任何类型的成员。
这是一个typeid
版本,它使用 boost::core::demangle
在运行时获取类型名称。
#include <string>
#include <iostream>
#include <typeinfo>
#include <vector>
using namespace std::literals;
#include <boost/core/demangle.hpp>
template<typename T>
std::string type_str(){ return boost::core::demangle(typeid(T).name()); }
auto main() -> int{
auto make_vector = [](auto head, auto ... tail) -> std::vector<decltype(head)>{
return {head, tail...};
};
auto i = 1;
auto f = 1.f;
auto d = 1.0;
auto s = "1.0"s;
auto v = make_vector(1, 2, 3, 4, 5);
std::cout
<< "typeof(i) = " << type_str<decltype(i)>() << 'n'
<< "typeof(f) = " << type_str<decltype(f)>() << 'n'
<< "typeof(d) = " << type_str<decltype(d)>() << 'n'
<< "typeof(s) = " << type_str<decltype(s)>() << 'n'
<< "typeof(v) = " << type_str<decltype(v)>() << 'n'
<< std::endl;
}
在我的系统上打印这个:
typeof(i) = int
typeof(f) = float
typeof(d) = double
typeof(s) = std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >
typeof(v) = std::vector<int, std::allocator<int> >
typeid 在大多数情况下可用于获取变量的类型。 它依赖于编译器,我已经看到它给出了奇怪的结果。 g++默认打开RTTI,不确定Windows端。
#include <iostream>
#include <typeinfo>
#include <stdint.h>
#include <chrono>
#include <ctime>
typedef std::ratio<1, 1> sec;
int main()
{
auto tickTime = .001;
std::chrono::duration<double, sec > timePerTick2{0.001};
auto nextTickTime = std::chrono::high_resolution_clock::now() + timePerTick2;
std::cout << typeid(tickTime).name() << std::endl;
std::cout << typeid(nextTickTime).name() << std::endl;
return 0;
}
./a.out | c++filt
double
std::__1::chrono::time_point<std::__1::chrono::steady_clock, std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000000000l> > >
一种低技术解决方案是将鼠标悬停在nextTickTime
上,在某些 GUI 中,它会在cout
中nextTickTime
后为其他类型设置一个.
并选择合理的外观值或函数。
一般来说,如果你知道你会使用什么类型,auto
如果你不知道它就不要使用它。这有点违反直觉。
因此,如果您知道它是一个交互器,只需使用自动来减少咒语,如果结果是某种未知类型,则必须在使用之前找出它是什么 auto
.
另见赫伯、安德烈和斯科特讨论auto
正如Daniel Jour所说,请阅读错误消息:
... _Tp = std::chrono::time_point<
std::chrono::_V2::system_clock,
std::chrono::duration<
double, std::ratio<1l, 1000000000l> > > ...
这是一种强制编译错误的方法,它显示了tickTime
的类型:
struct {} baD = tickTime;
编译器推导的类型在错误消息中:
/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> > >]’
^^ <-------- the long type name --------------------------------------------------------------------------------------->
这是一个复杂的类型名称,但它存在于错误消息中。
将其添加到代码中:
decltype(whateverYouWantTheTypeOf)::_;
您的编译器会给您一个错误,将显示whateverYouWantTheTypeOf
的类型(甚至可以是表达式(。示例错误:
# clang++ error on something defined as auto
type 'decltype(length)' (aka 'long') cannot be used prior to '::' because it has no members
decltype(length)::_;
# g++ error on an expression
error: ‘_’ is not a member of ‘__gnu_cxx::__normal_iterator<float*, std::vector<float> >::difference_type’ {aka ‘long int’}
27 | decltype(last - first)::_;
作为旁注,为了有效地打印出nextTickTime
中的值,您应该显式转换为合适的std::chrono::duration
并输出duration::count
的结果。
using std::chrono::duration_cast;
using std::chrono::seconds;
auto baseTime = ...;
std::cout << std::setprecision(12) << duration_cast<seconds>(nextTickTime - baseTime).count()
<< std::endl; // time in seconds
@jonathan-oconnor指出,您可以使用C++14中引入的[[deprecated]]
属性来生成一个非常干净的解决方案:
template<typename T>
[[deprecated]] constexpr void printType(T const&) {}
不幸的是,MSVC 发出的诊断没有提到类型。(示例(