【问题标题】:clang++ error on late default template parameter declaration延迟默认模板参数声明时出现 clang++ 错误
【发布时间】:2015-04-21 15:38:03
【问题描述】:

以下代码使用 g++ 编译良好,但使用 clang++ (3.6) 编译失败:

// Forward declaration:
template <class S, class T>
struct Base;

template <class T>
struct BaseFriend {
    friend struct Base<int, T>;
};

// Actual declaration:
template <class S, class T = int>
struct Base {
    void foo() {}
};

struct DerivedFriend : BaseFriend<int> {};

struct Derived : Base<int> {
    void foo(int) {
        Base<int>::foo();
    }
};

Derived::foo 定义出现错误:

error: too few template arguments for class template 'Base'
    Base<int>::foo();
    ^
test.cpp:3:8: note: template is declared here
struct Base;
       ^

经过几次小修复后错误消失,例如:

  1. 如果在前向声明而不是实际声明中定义了默认模板参数。
  2. 或者如果没有使用DerivedFriend。

但是,原来的代码有什么问题?

【问题讨论】:

  • 这看起来像一个clang错误。
  • 归档 bug report -- 这显然是一个 clang 错误,因为 GCC 4.9、ICC 13 和 MSVC 17 (VS2012) 都接受该代码。
  • @Barry,这很奇怪,因为它在 3.5 上对我来说也失败了......(我在 RadHat 6.3 上)
  • @Barry 我刚刚在 Coliru 上尝试过,它给了我同样的错误......

标签: c++ templates clang++


【解决方案1】:

绝对是一个 clang 错误,看起来像 #10147。标准明确允许这个 [temp.param]/10:

获得可用于模板声明或定义的默认模板参数集 通过合并定义中的默认参数(如果在范围内)和同一范围内的所有声明 方式默认函数参数是(8.3.6)。 [示例:

template<class T1, class T2 = int> class A;
template<class T1 = int, class T2> class A;

等价于

template<class T1 = int, class T2 = int> class A;

——结束示例]

【讨论】:

  • 是的,它肯定在 #10147 的范围内,尽管从查看该错误报告来看并不相同......
猜你喜欢
  • 1970-01-01
  • 2014-01-19
  • 2010-12-20
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
相关资源
最近更新 更多