部分特化非顶级cv限定符在函数签名中被忽略

Partial specialization non-top level cv qualifier ignored in function signature

本文关键字:函数 cv      更新时间:2023-10-16

我试着使用下面的测试用例:

/* Generic implementation */
template <typename T>
struct SpecWrapper {
   static void bar(T const* src) {
      printf("src[0] = %len", src[0]);
   }
}; 
/* Volatile partial-specialization */
template <typename T>
struct SpecWrapper<T volatile> {
   static void bar(T const* src) {
      printf("src[0] = %len", src[0]);
   }  
}; 
/* Instantiate */
void foo(double volatile const* src) {
   SpecWrapper<double volatile>::bar(src);
}

但是使用g++

会产生以下错误
test.cxx: In function ‘void foo(const volatile double*)’:
test.cxx:18:38: error: invalid conversion from ‘const volatile double*’ to ‘const double*’ [-fpermissive]
    LowLevel<double volatile>::bar(src);
                                      ^
test.cxx:12:16: error:   initializing argument 1 of ‘static void LowLevel<volatile T>::bar(const T*) [with T = double]’ [-fpermissive]
    static void bar(T const* src) {
                ^
谁能解释一下为什么会出现这个问题?我想到了一些变通方法,但我想首先理解为什么这是一个问题。

应该是

/* Volatile partial-specialization */
template <typename T>
struct SpecWrapper<T volatile> {
   static void bar(T volatile const* src) {
      printf("src[0] = %len", src[0]);
   }  
};

因为T就是double