【发布时间】: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;
^
经过几次小修复后错误消失,例如:
- 如果在前向声明而不是实际声明中定义了默认模板参数。
- 或者如果没有使用
DerivedFriend。
但是,原来的代码有什么问题?
【问题讨论】:
-
这看起来像一个clang错误。
-
归档 bug report -- 这显然是一个 clang 错误,因为 GCC 4.9、ICC 13 和 MSVC 17 (VS2012) 都接受该代码。
-
@Barry,这很奇怪,因为它在 3.5 上对我来说也失败了......(我在 RadHat 6.3 上)
-
@Barry 我刚刚在 Coliru 上尝试过,它给了我同样的错误......