【问题标题】:Why the error in this simple template code?为什么这个简单的模板代码会出错?
【发布时间】:2013-07-10 15:27:52
【问题描述】:

为什么会出现下面的错误?

#include <type_traits>

template<typename FooType>
struct bar {
  using bar_type = typename FooType::foo_type;
};

template<typename T>
struct foo {
  using foo_type = T;

  //Error: No type named 'bar_type' in 'bar<foo<int> >'
  static_assert(std::is_same<foo_type,typename bar<foo<T>>::bar_type>::value,"");
};

int main(int argc, char **argv)
{
  bar<foo<int>> b;
  return 0;
}

【问题讨论】:

标签: c++ templates


【解决方案1】:

static_assert 处,foo 还不是一个完整的类型(您仍在其定义范围内),因此当bar 尝试进入时,编译器会给您一个错误。它给你的特定错误很糟糕;尝试使用不同的编译器。

【讨论】:

  • 正如 Johannes Schaub 在他的 answer 中指出的我在评论中链接到 OP 的问题,标准甚至可能 未明确指定。是否非法。
  • 有趣,我不知道。刚看了代码,想到了Clang的实例化模型,描述了我看到的问题。
猜你喜欢
  • 1970-01-01
  • 2015-02-19
  • 1970-01-01
  • 1970-01-01
  • 2011-03-03
  • 2018-02-23
  • 2012-07-13
  • 2017-06-05
  • 1970-01-01
相关资源
最近更新 更多