在浏览gcc当前新C++ 11标头的实现时,我偶然发现了"......"令牌.您可以检查,以下代码编译正常 [通过ideone.com].
template <typename T>
struct X
{ /* ... */ };
template <typename T, typename ... U>
struct X<T(U......)> // this line is the important one
{ /* ... */ };
Run Code Online (Sandbox Code Playgroud)
那么,这个令牌的含义是什么?
编辑:看起来如此修剪"......"的问题标题为"......",我的意思是"......".:)
受这个答案的启发,我生成了这段代码,其输出取决于编译器:
template <typename... Args>
constexpr auto foo(Args&& ...args, ...) noexcept {
return sizeof...(args);
}
constexpr auto bar() noexcept {
return (&foo<int>)(1, 2);
}
Run Code Online (Sandbox Code Playgroud)
如果使用 GCC 11 编译,bar则调用foo<int>并返回 1,而 clang 13 和 MSVC 2019 都会推导foo<int, int>并bar返回 2。
这是我在 godbolt 上的沙箱:https ://godbolt.org/z/MedvvbzqG 。
哪个输出是正确的?
编辑:
如果我return foo<int>(1, 2);直接使用,即与
constexpr auto bar() noexcept {
return foo<int>(1, 2);
}
Run Code Online (Sandbox Code Playgroud)
c++ variadic-functions language-lawyer variadic-templates template-argument-deduction