C++编译时输出typedef的类型(特别是发生错误时) [英] Output the type of a typedef at compile time in C++ (specifically when an error occurs)

查看:37
本文介绍了C++编译时输出typedef的类型(特别是发生错误时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难解释这个特定的问题,所以请耐心等待(我的所有模板相关问题都有问题!).

以这段代码为例(注意,展示代码的目的是展示一个复杂的模板层次结构,而不是它是否有意义):

Take this code as an example (note that the point of showing the code is to show a complex template hierarchy, not whether it makes sense):

#include <string>
#include <vector>
#include <list>

template <typename T>
struct Foo
{
  typedef typename T::value_type value_type;
  typedef typename T::value_type1 value_type1;
  typedef typename T::value_type2 value_type2;

  Foo() {}
  Foo(value_type1, value_type, value_type2) {}
  Foo(value_type, value_type1, value_type2) {}
};

template <typename T, typename T1, typename T2>
struct MiddleMan
{
  typedef T   value_type;
  typedef T1  value_type1;
  typedef T2  value_type2;
};

template <typename T>
struct MainClass
{
  typedef typename T::value_type value_type;
  typedef typename T::value_type1 value_type1;
  typedef typename T::value_type2 value_type2;

  typedef MainClass<T>  this_type;

  typedef Foo<this_type> iterator;
};

using namespace std;

int main()
{
  typedef MiddleMan<string, vector<string>, list<vector<string> > > mm;
  MainClass<mm>::iterator  a(1, 2, 3);

  return 0;
}

并假设这是您遇到的错误

and assume this is the error you are getting

三个重载都不能转换所有的参数类型

None of the 3 overloads could convert all the argument types

请注意,我知道在这种情况下,如果您编译代码,则错误消息与上述不同,但我目前正在处理的复杂模板代码中的错误消息是上述错误消息.我刚刚提出了一个简化的例子来帮助解决这个问题.

Note that I know in this instance, if you compile the code, the error message is not the same as above but the error message in my complex template code that I am currently working on is the above. I just presented a simplified example to help with the question.

现在我想知道Foo的类型,即value_typevalue_type1value_type2,以便我可以修复错误,而无需一直追溯到 MiddleMan.我不想手动跟踪的原因是代码可能是非常复杂的模板代码,很难进行回溯.

Now I want to know the types of Foo, i.e. value_type, value_type1, value_type2 so that I can fix the error, without tracing back all the way to MiddleMan. The reason I don't want to trace manually is that the code can be quite complex template code where a trace back is very difficult to do.

我想既然编译器已经弄清楚了类型,它应该能够让我知道,即应该有一种简单的方法可以在编译时(可能通过输出窗口中的消息)确定类型附加到 typedefs.有没有简单的方法?

I figured since the compiler has already figured out the types, it should be able to let me know, i.e. there should be an easy way to figure it out at compile time (maybe through a message in the output window) the types attached to the typedefs. Is there an easy way?

解决方案这是另一个示例,在阅读所选答案后可能会对未来的 SOer 有所帮助:

SOLUTION- Here is another example that may help future SOer after reading the selected answer:

template <typename T> struct incomplete;

template <typename T, typename T2, typename T3>
class foo
{
public:
  typedef T value_type;
  typedef T2 value_type2;
  typedef T3 value_type3;
};

int main()
{
  // Assume the following type is much more complex
  typedef foo<float, int, char> type_i_am_having_trouble_with;

  // At this point you are instantiating it, and the program compiles (or maybe
  // not) and you have no idea what some of the typedefs stand for
  type_i_am_having_trouble_with b;

  // Use this to find out what the typedefs stand for
  incomplete<type_i_am_having_trouble_with::value_type> test; 
}

Visual C++ 2008 的输出:

Output from Visual C++ 2008:

error C2079: 'test' uses undefined struct 'incomplete<T>'
1>        with
1>        [
1>            T=float
1>        ]

推荐答案

有一些技巧可以让编译器向您显示 typedef 的实际类型.一种是尝试实例化一个不完整的类型.

There are a few tricks to get the compiler to show you the actual type of a typedef. One is to try to instantiate an incomplete type.

template<typename>
struct Printer;

typedef std::vector<int> foobartype;
Printer<foobartype> printer;

还有 boost::mpl::print 实际上可以发出警告而不是出错.

There is also boost::mpl::print which can actually issue a warning instead of erroring out.

所有这些技术都必须在实际可以访问类型名称的地方使用,因此您最终必须跟踪"代码.

All those techniques have to be used at the place where the typename is actually accessible so you will have to 'trace' through the code eventually.

遗憾的是,调试模板代码几乎是一门黑色艺术,通常您必须玩编译器并在脑海中实例化一些东西才能解决问题.

Sadly, debugging template code is pretty much a black art and often you will have to play compiler and instantiate things in your head to get to the issue.

这篇关于C++编译时输出typedef的类型(特别是发生错误时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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