相关疑难解决方法(0)

"......"令牌是什么意思?即参数包上的双省略运算符

在浏览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)

那么,这个令牌的含义是什么?

编辑:看起来如此修剪"......"的问题标题为"......",我的意思是"......".:)

c++ variadic-templates c++11

110
推荐指数
1
解决办法
7513
查看次数

将可变参数模板与 C 风格可变参数函数混合时的模板参数推导

受这个答案的启发,我生成了这段代码,其输出取决于编译器:

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)

沙箱更新:https://godbolt.org/z/Wj757sc7b。

c++ variadic-functions language-lawyer variadic-templates template-argument-deduction

7
推荐指数
1
解决办法
306
查看次数