强制编译器显示变量的类型

bar*_*rej 5 c++ compiler-errors compilation c++11

我正在编制一个我面临的相当大的项目

错误:'CRoom room'重新声明为不同类型的符号

对了

class CRoom
{
.....
} room("test");
Run Code Online (Sandbox Code Playgroud)

问题是我搜索了整个项目文件,我在其他地方找不到这样的变量.是否有可能强迫编制者告诉我它在哪里找到了这种定义的原始位置?如果不可能,至少可以在comfile时间显示原始变量的类型(注意,这个程序有很多其他错误,我不能运行它并显示变量类型.我希望编译器显示类型为了我).

Rei*_*ica 10

为了让编译器向我展示某些东西的类型,我通常使用这样的improptu类:

template <class T>
struct show_type;
Run Code Online (Sandbox Code Playgroud)

然后,在您需要学习类型的代码中,您将执行以下操作:

show_type<decltype(room)> t;
Run Code Online (Sandbox Code Playgroud)

编译它,编译器会正确地抱怨show_type<T>没有定义; 但错误消息将有助于说明T尝试实例化的时间.

  • 为什么要定义`show_type`并使用`:: type`呢?`template <class T> struct show_type; show_type <decltype(room)> t;` (4认同)

Pra*_*ian 8

这可以通过声明一个类模板并使其未实现来轻松完成.

template<typename T>
struct dump;

dump<decltype(room)> d;
Run Code Online (Sandbox Code Playgroud)

这将分别在gcc和clang上产生以下错误消息

error: aggregate 'dump<CRoom> d' has incomplete type and cannot be defined
error: implicit instantiation of undefined template 'dump<CRoom>'
Run Code Online (Sandbox Code Playgroud)

现场演示

允许程序编译的另一种方法是使用Boost.TypeIndex

#include <boost/type_index.hpp>

std::cout << boost::typeindex::type_id_with_cvr<decltype(room)>().pretty_name() << '\n';
Run Code Online (Sandbox Code Playgroud)

这会在CRoomgcc和clang上产生输出

现场演示