2014-07-18 21 views
3

我想使用類似下面的測試案例:偏特非頂級CV預選賽忽略函數簽名

/* Generic implementation */ 
template <typename T> 
struct SpecWrapper { 
    static void bar(T const* src) { 
     printf("src[0] = %le\n", src[0]); 
    } 
}; 

/* Volatile partial-specialization */ 
template <typename T> 
struct SpecWrapper<T volatile> { 
    static void bar(T const* src) { 
     printf("src[0] = %le\n", 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) { 
       ^

以下錯誤有人可以解釋爲什麼會出現這個問題嗎?有幾個解決辦法值得思考,但我想先了解爲什麼這是一個問題。

回答

2

應該

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

因爲T只是double