【发布时间】:2017-03-17 09:56:36
【问题描述】:
我尝试构建一个不需要 typename 或 template 的案例,但仍会根据给定名称 t 是否为函数来生成变量或模板参数包与否
template<typename T> struct A { template<int> static void f(int) { } };
template<typename...T> struct A<void(T...,...)> { static const int f = 0; };
template<typename> using type = int;
template<typename T> void f(T t) { A<void(type<decltype(t)>...)>::f<0>(1); }
int main() {
f(1);
}
以上将参考static const int,并做一个对比。以下只是将T t 更改为一个包并使f 引用一个模板,但GCC 也不喜欢
template<typename ...T> void f(T ...t) { A<void(type<decltype(t)>...)>::f<0>(1); }
int main() {
f(1, 2, 3);
}
GCC 第一次抱怨
main.cpp:5:68: error: incomplete type 'A<void(type<decltype (t)>, ...)>' used in nested name specifier
template<typename T> void f(T t) { A<void(type<decltype(t)>...)>::f<0>(1); }
第二个
main.cpp:5:74: error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator<'
template<typename ...T> void f(T ...t) { A<void(type<decltype(t)>...)>::f<0>(1); }
我有多个问题
- 以上代码是否根据语言工作,还是有错误?
- 由于 Clang 接受这两种变体但 GCC 拒绝,我想问什么编译器是正确的?
-
如果我删除了主模板的主体,那么对于
f(1, 2, 3)的情况,Clang 会抱怨main.cpp:5:42: error: implicit instantiation of undefined template 'A<void (int)>'请注意,上面写着
A<void (int) >,而我期望的是A<void (int, int, int)>。这种行为是如何发生的?这是我的代码中的错误 - 即它是错误的,还是 Clang 中的错误?我似乎记得一个关于扩展顺序与别名模板替换的缺陷报告,这是否相关,是否会使我的代码格式错误?
【问题讨论】:
-
对我来说
void(T...,...)看起来像T...在非推断上下文中,但我不确定编译器应该如何反应...... -
@W.F.我在“is_function”特征实现中看到了这一点,以检测具有该模式的函数。如果这不能推断出
T,我会感到惊讶,因为我不确定is_function应该如何检测它们。 -
不知道我需要在那里看看
-
在
f是template的情况下,为什么在::和f之间不需要template? -
@Yakk 我的印象是
type<dependent-type>不依赖于template<typename> using type = int;,但我可能是错的。见open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1390。问这个问题的时候忘记了很久以前做的那个DR了,现在才发现其实还没有解决。委员会只是澄清他们希望它是非依赖的。但是,除此之外,我的 sn-p 还有什么问题吗?
标签: c++ c++11 language-lawyer