Tak*_*ndo 9 c++ gcc templates clang
当我在注释行上编译以下代码时,我得到编译错误"错误:使用'template'关键字将'foo'视为依赖模板名称".(TEST4)
代码的所有其他部分都已成功编译.
#include <tuple>
struct my {
template <typename T>
void foo() {}
};
void test1() {
my m;
auto tr = std::forward_as_tuple(m);
auto& t0 = std::get<0>(tr);
t0.foo<int>();
}
template <typename T>
struct test2 {
void method() {
my m;
auto tr = std::forward_as_tuple(m);
auto& t0 = std::get<0>(tr);
t0.foo<int>();
}
};
template <typename T>
struct test3 {
void method() {
m.foo<int>();
}
my m;
};
template <typename T>
struct test4 {
void method() {
auto tr = std::forward_as_tuple(m);
auto& t0 = std::get<0>(tr);
t0.foo<int>(); // error: use 'template' keyword to treat 'foo' as a dependent template name
t0.template foo<int>(); // OK
}
my m;
};
template <typename T>
struct test5 {
void method() {
std::tuple<my> tr = std::forward_as_tuple(m);
auto& t0 = std::get<0>(tr);
t0.foo<int>();
}
my m;
};
template <typename T>
struct test6 {
void method() {
auto tr = std::forward_as_tuple(m);
my& t0 = std::get<0>(tr);
t0.foo<int>();
}
my m;
};
int main() {
test1();
test2<int>().method();
test3<int>().method();
test4<int>().method();
test5<int>().method();
test6<int>().method();
}
Run Code Online (Sandbox Code Playgroud)
test4
是一个类模板,但m是非依赖类型.
我试着编译gcc和clang.gcc 7.1.0不报告错误,但是clang 4.0及更高版本报告了编译错误.
错误
https://wandbox.org/permlink/HTSBJMD2kXwfWObl(clang 4.0) https://wandbox.org/permlink/BcUT8gtaFxwC41c5 (clang HEAD)
没错
https://wandbox.org/permlink/GjIvZa3i5HB8uh6w(gcc 7.1.0)
哪种行为正确?
我同意你的怀疑。这确实是一个 clang bug。
template
当且仅当t0
是从属名称时才是必需的。t0
特别是,如果依赖于则需要它T
。这T
就是test4<T>
.
现在,t0
取决于my m
,并且有一个my::foo<T>
,但T
在不同的范围内这是一个不相关的。此外,t0
不依赖于my::foo<T>
.