c++ - 为什么对指针参数包进行 const 限定是错误的?

标签 c++ c++11 constants variadic-functions

当实现一个接受指向 Ts... 的指针参数包的函数时,为什么我不能像常规的那样对指针进行 const 限定参数?

我在最新的 GCC 和 Clang 上得到一个不匹配的签名错误,我不明白为什么,因为指针是 const 只是一个实现细节(因此它对于常规参数是合法的)。

template<typename... Ts>
class C
{
    void f(int*);
    void g(Ts*...);
};

template<typename... Ts>
void C<Ts...>::f(int* const) {} // Legal

template<typename... Ts>
void C<Ts...>::g(Ts* const...) {} // Compiler error

我收到此错误:

prog.cc:12:16: error: out-of-line definition of 'g' does not match any declaration in 'C<Ts...>'
void C<Ts...>::g(Ts* const...) {}
               ^
1 error generated.

您还可以看到代码和错误here .

最佳答案

我将把它归结为一对编译器错误(经过测试的 Clang 和 GCC)。我知道这是一个大胆的断言,但是 [dcl.fct]/5说,强调我的:

A single name can be used for several different functions in a single scope; this is function overloading. All declarations for a function shall agree exactly in both the return type and the parameter-type-list. The type of a function is determined using the following rules. The type of each parameter (including function parameter packs) is determined from its own decl-specifier-seq and declarator. After determining the type of each parameter, any parameter of type “array of T” or of function type T is adjusted to be “pointer to T”. After producing the list of parameter types, any top-level cv-qualifiers modifying a parameter type are deleted when forming the function type. The resulting list of transformed parameter types and the presence or absence of the ellipsis or a function parameter pack is the function's parameter-type-list. [ Note: This transformation does not affect the types of the parameters. For example, int()(const int p, decltype(p)) and int()(int, const int) are identical types.  — end note ]

这对我来说很清楚,两个成员(f g)的声明都与类外匹配定义,使您的程序有效。所以 Clang 和 GCC 应该接受它。

关于c++ - 为什么对指针参数包进行 const 限定是错误的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49403093/

相关文章:

c++ - 取消引用迭代器时出现段错误

c++ - C++ 中共享库中对象的静态常量。进程间共享吗?

Javadoc {@value} 不适用于常量

c++ - 整数输出格式。 printf(%.3x) 的类似物是什么?

c++ - 如何替换文本文件中的多个字符串? C++

c++11 type_traits : different result in INTEL 2013 and GCC 4. 7.2

c++ - 了解工厂方法和静态变量分配的返回值优化 (Visual Studio)

c++ - 我无法初始化 C 类的 shared_ptr

c++ - 避免重复 const 和非 const 版本的 getter?

安卓调试ndk : permission denied