如何通过自动显式查看类型推断的结果? [英] How can I explicitly view the results of type inference by auto?

查看:87
本文介绍了如何通过自动显式查看类型推断的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在新学习C ++ 11/14的自动功能。

I am newly studying auto feature of C++11/14.

出于教育目的,我想明确显示代码类型推断的结果。
我尝试了typeid()。name(),但是我发现这种方法有两个问题。

For educational purpose, I would like to explicitly display the result of type inference of my code. I tried typeid().name(), but I found two problems with this approach.


  1. 有时输出很难理解。 (例如, NSt3__16vectorIiNS_9allocatorIiEEEE)

  2. const / volatile修饰符似乎未显示。

@πάνταῥεῖ我尝试使用您指出的abi :: __ cxa_demangle()。
问题1已解决,谢谢,但是typeid()。name()似乎不包含CV修饰符信息。
我认为使用 auto 关键字有一些陷阱,所以我想看看类型推断的确切结果,包括CV修饰符和引用类型。

@πάνταῥεῖ I have tried using abi::__cxa_demangle() you pointed out. Problem 1 is solved, thank you, but typeid().name() does not seem to contain CV modifier information. I think there are some pitfalls using auto keyword, so I would like to see the exact result of the type inference, including CV modifier and reference type.

我在Mac OS 10.10.3上使用clang 6.1.0,但我想知道便携式方法,如果可能的话。

I am using clang 6.1.0 on mac os 10.10.3, but I would like to know portable way to do this, if possible.

推荐答案

尝试Scott Meyers(Effective Modern C ++)提出的方法:

Try the approach proposed by Scott Meyers (Effective Modern C++):

声明模板(但

template<typename T>       // declaration only for TD;
class TD;                  // TD == "Type Displayer"

然后使用您的类型实例化此模板

Then instantiate this template using your type

TD<decltype(x)> xType

编译器现在会抱怨这个不完整的类型(通常会显示它的全名)

The compiler will now complain about this incomplete type (and usually will display the full name of it)


错误:汇总'TD< int> xType'具有不完整的类型,无法定义

error: aggregate 'TD< int > xType' has incomplete type and cannot be defined

请参阅有效的现代C ++第4项(通常我建议这样做)一本书作为必读)

See Item 4 of "Effective Modern C++" (generally I'd propose this book as a "must read")

这篇关于如何通过自动显式查看类型推断的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆