我试图使用类似于以下测试用例的东西:
/* 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) {
^有人能解释一下为什么会出现这个问题吗?这个春天有几个解决办法,但我想先了解一下为什么这是个问题。
发布于 2014-07-18 11:42:44
应该是
/* 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。
https://stackoverflow.com/questions/24824138
复制相似问题