限制指针类型模板参数和覆盖模板化基的虚方法 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。我做错了什么?

备注:

__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__ 而将其保留在函数定义本身中似乎可以实现您想要的效果。