限制指针类型模板参数和覆盖模板化基的虚方法 class
restrict-pointer-type template arguments and overriding virtual methods of a templated base class
我相信以下应该编译和 link,但没有:
template<class S>
class A {
public:
virtual int foo(S arg) = 0;
virtual ~A() { }
};
class B : public A<int* __restrict__>
{
public:
int foo(int* __restrict__ arg) override { return 0; }
};
int main() { B b; }
编译器输出:
d9.cpp:11:6: error: ‘int B::foo(int*)’ marked override, but does not override
int foo(int* __restrict__ arg) override { return 0; }
^
d9.cpp: In function ‘int main()’:
d9.cpp:14:16: error: cannot declare variable ‘b’ to be of abstract type ‘B’
int main() { B b; }
^
d9.cpp:8:7: note: because the following virtual functions are pure within ‘B’:
class B : public A<int* __restrict__>
^
d9.cpp:4:14: note: int A<S>::foo(S) [with S = int* __restrict__]
virtual int foo(S arg) = 0;
如果我在两个地方都删除了 __restrict__
限定符,它会编译并 link。我做错了什么?
备注:
- 这是关于限制限定符和模板的 唯一问题(截至撰写本文时)。很有趣,不是吗?
- 我正在使用 GCC 4.9.3
--std=c++11
。
__restrict__
关键字似乎并没有真正创建新类型:
As with all outermost parameter qualifiers, __restrict__
is ignored in
function definition matching. This means you only need to specify
__restrict__
in a function definition, rather than in a function prototype as well.
https://gcc.gnu.org/onlinedocs/gcc/Restricted-Pointers.html
在模板参数和纯虚函数定义中删除 __restrict__
而将其保留在函数定义本身中似乎可以实现您想要的效果。
我相信以下应该编译和 link,但没有:
template<class S>
class A {
public:
virtual int foo(S arg) = 0;
virtual ~A() { }
};
class B : public A<int* __restrict__>
{
public:
int foo(int* __restrict__ arg) override { return 0; }
};
int main() { B b; }
编译器输出:
d9.cpp:11:6: error: ‘int B::foo(int*)’ marked override, but does not override
int foo(int* __restrict__ arg) override { return 0; }
^
d9.cpp: In function ‘int main()’:
d9.cpp:14:16: error: cannot declare variable ‘b’ to be of abstract type ‘B’
int main() { B b; }
^
d9.cpp:8:7: note: because the following virtual functions are pure within ‘B’:
class B : public A<int* __restrict__>
^
d9.cpp:4:14: note: int A<S>::foo(S) [with S = int* __restrict__]
virtual int foo(S arg) = 0;
如果我在两个地方都删除了 __restrict__
限定符,它会编译并 link。我做错了什么?
备注:
- 这是关于限制限定符和模板的 唯一问题(截至撰写本文时)。很有趣,不是吗?
- 我正在使用 GCC 4.9.3
--std=c++11
。
__restrict__
关键字似乎并没有真正创建新类型:
As with all outermost parameter qualifiers,
__restrict__
is ignored in function definition matching. This means you only need to specify__restrict__
in a function definition, rather than in a function prototype as well.
https://gcc.gnu.org/onlinedocs/gcc/Restricted-Pointers.html
在模板参数和纯虚函数定义中删除 __restrict__
而将其保留在函数定义本身中似乎可以实现您想要的效果。