【问题标题】:Forward declaring the mapped type of a map and C++11 [duplicate]前向声明映射的映射类型和 C++11 [重复]
【发布时间】:2013-07-28 09:04:36
【问题描述】:

我使用过的大多数 C++ 编译器都接受以下内容

#include <map>

struct A;

struct B
{
    typedef std::map<int,A>::iterator iterator;
    std::map<int,A> test;
};

struct A
{
};

int main()
{
    return 0;
}

然而,Apple clang 4.0 编译时使用

clang++ test.cpp -o test -std=c++11 -stdlib=libc++

产生一组错误,暗示 A 必须是完整类型,然后才能使用 std::map。这是 map 的 libc++ 实现中的缺陷、C++11 强加的新要求还是我的错误假设?

【问题讨论】:

  • 更新:上面的代码在 clang++-3.8.0 [删除-stdlib=libc++] 后编译得很好。

标签: c++ c++11 clang


【解决方案1】:

根据 17.6.4.8 [res.on.functions] 第 2 段中的要求,它指出:

特别是,在以下情况下效果未定义:...如果在实例化模板组件时将不完整的类型 (3.9) 用作模板参数,除非该组件特别允许。

很少有组件明确声明允许模板参数不完整。也就是说,您是否正在做出标准未涵盖的假设。

【讨论】:

  • 我以为会是这样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多