Force the compiler to specify the type of variable

I am compiling a rather large project in which I came across

error: "CRoom room is redefined as another character

Right on

class CRoom { ..... } room("test"); 

The problem is that I was looking for all my project files, and I could not find such a variable anywhere. Is it possible to make the interlocutor tell me where he found the starting point of such a definition? If this is not possible, at least you can specify the type of the source variable during comfile (note that these programs have so many other errors, and I cannot run it and show the type of the variable. I want the compiler to show the type for me )

+5
source share
2 answers

To make the compiler show me the type of something, I usually use the improptu class as follows:

 template <class T> struct show_type; 

Then, in the code where you need to examine the type, you will do the following:

 show_type<decltype(room)> t; 

Compile this, and the compiler will rightfully complain that show_type<T> is undefined; but in the error message it will be useful to indicate the T with which an attempt was made to create an instance.

+10
source

This can be done easily by declaring the class template and leaving it unrealized.

 template<typename T> struct dump; dump<decltype(room)> d; 

This will result in the following error messages on gcc and clang, respectively

 error: aggregate 'dump<CRoom> d' has incomplete type and cannot be defined error: implicit instantiation of undefined template 'dump<CRoom>' 

Live demo

An alternative that allows you to compile a program is to use Boost.TypeIndex

 #include <boost/type_index.hpp> std::cout << boost::typeindex::type_id_with_cvr<decltype(room)>().pretty_name() << '\n'; 

This leads to the output of CRoom on gcc and clang

Live demo

+8
source

Source: https://habr.com/ru/post/1212535/


All Articles