gpt4 book ai didi

c++ - 在 friend 子类中使用基类的私有(private)方法——NVCC 中的编译器错误?

转载 作者:行者123 更新时间:2023-11-30 03:35:17 25 4
gpt4 key购买 nike

在尝试使用英特尔线程构建模块编译一些 CUDA 代码时,我发现了我认为是 nvcc 中的错误。以下最小示例使用 g++ 5.4 编译良好:

class Sub;
class Other;

namespace internal {
class Base
{
private:
friend class ::Sub;

static void foo(::Sub& b);
static void foo(::Other& c);
};
}

class Sub : private internal::Base
{
public:
using internal::Base::foo;
};

void internal::Base::foo(Sub& b)
{
}

int main(int argc, char *argv[])
{
Sub *b;
b->foo(*b);
// Sub::foo(*b);

return 0;
}

但是如果我使用 nvcc 8.0 结合相同的主机编译器编译它,使用

nvcc -x cu -arch=sm_35 -c minimal.cc

我收到以下有趣的错误:

../minimal.cc: In function ‘int main(int, char**)’:
../minimal.cc:28:21: error: ‘internal::Base’ is an inaccessible base of ‘Sub’

如果将 Base 移出 internal 命名空间并移至全局命名空间,则会出现更具描述性的错误:

../minimal.cc: In function ‘int main(int, char**)’:
../minimal.cc:6:12: error: ‘class Base Base::Base’ is inaccessible
class Base
^
../minimal.cc:32:5: error: within this context
b->foo(*b);
^
../minimal.cc:32:11: error: ‘Base’ is an inaccessible base of ‘Sub’
b->foo(*b);

很明显,这似乎是由于使用指针调用静态方法的方式有点不标准,如果该行被注释掉的行替换,它编译得很好。

有人可以确认这是有效的 C++ 并因此是 nvcc 中的错误,还是无效的 C++ g++ 仍然乐于接受?

最佳答案

我对此进行了更多研究,发现它确实是 nvcc 的其中一个阶段的问题。汇编。使用 --save-temps ,并检查结果 .cu.cpp.ii文件,结果是这一行

b->foo(*b);

被下面的替换

(b->internal::Base::foo(*b));

这不能用 g++ 编译,从那以后的事实fooSub 中作为公共(public)导出丢失了。毕竟,这试图从它所在的基类显式访问它 private .使用其他类型的调用 (Sub::foo) 不会导致生成任何额外的代码。

我得出结论,这是 nvcc 中的错误.有趣的是,如果第二个重载 void foo(::Other &c),此替换不会发生未在 Base 中声明.

关于c++ - 在 friend 子类中使用基类的私有(private)方法——NVCC 中的编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41380602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com