【发布时间】:2018-08-20 11:12:20
【问题描述】:
以下 C++ 文件:
struct Base {
template <typename T, int = 42>
void f(T &&) const {}
};
struct Derived: Base {
template <typename T, typename X = typename T::asdf>
void f(T &&) const {}
using Base::f;
};
int main() {
Derived const cd;
cd.f('x');
}
用 GCC 编译得很好,但不能用 Clang:
$ g++-7.3.0 -std=c++11 test.cpp -o test -Wall -Wextra
$ g++-7.2.0 -std=c++11 test.cpp -o test -Wall -Wextra
$ g++-6.4.0 -std=c++11 test.cpp -o test -Wall -Wextra
$ g++-5.4.0 -std=c++11 test.cpp -o test -Wall -Wextra
$ g++-4.9.4 -std=c++11 test.cpp -o test -Wall -Wextra
$ clang++-4.0 -std=c++11 test.cpp -o test -Wall -Wextra
test.cpp:15:12: error: no matching member function for call to 'f'
cd.f('x');
~~~^
test.cpp:8:14: note: candidate template ignored: substitution failure [with T = char]: type 'char' cannot be used prior to '::' because it has no members
void f(T &&) const {}
^
1 error generated.
$ clang++-5.0 -std=c++11 test.cpp -o test -Wall -Wextra
test.cpp:15:12: error: no matching member function for call to 'f'
cd.f('x');
~~~^
test.cpp:8:14: note: candidate template ignored: substitution failure [with T = char]: type 'char' cannot be used prior to '::' because it has no members
void f(T &&) const {}
^
1 error generated.
$ clang++-6.0 -std=c++11 test.cpp -o test -Wall -Wextra
test.cpp:15:12: error: no matching member function for call to 'f'
cd.f('x');
~~~^
test.cpp:8:14: note: candidate template ignored: substitution failure [with T = char]: type 'char' cannot be used prior to '::' because it has no members
void f(T &&) const {}
^
1 error generated.
为什么不能用 Clang 编译?我的代码正确吗?这是编译器错误吗? C++ 标准中的错误?
【问题讨论】:
-
绝不是狼疮,也绝不是编译器错误。没有匹配
cd.f('x');'char::asdf` 不是一个东西。你想做什么? -
@JiveDadson:肯定是其中之一的编译器错误 - 代码 sn-p 违反标准,它应该是错误(使用 GCC 成功编译的编译器错误)或代码 sn- p 很好,不应该出错(Clang 的编译器错误导致编译不成功)。
-
代码不好。它不使用 Base,因为用户代码明确表示 Derived。
-
今天我不合时宜...是的:你已经添加了
using Base::f...对不起...忘记我说过的话。我不知道 SFINAE 禁用功能是否涵盖基类(如认为 liliscent )。再次抱歉。
标签: c++ c++11 gcc clang compiler-bug