小编Eri*_*ric的帖子

如何使用 clang 将模板类的构造函数声明为友元?(使用 g++ 编译,而不是 clang++)

我有一个带有私有构造函数的模板类,它是该类的每个类型实例的朋友。以下在 g++ 11.4.0 下编译,但在 clang++ 版本 14.0.0-1ubuntu1.1 下失败

template <typename T>
class foo {
    foo(T){}
    template <typename U> friend foo<U>::foo(U);

    public:
    foo(){}
};

int main() {
    foo<int> a{};
}
Run Code Online (Sandbox Code Playgroud)

clang 给出了错误

main.cpp:4:34: error: missing 'typename' prior to dependent type name 'foo<U>::foo'
    template <typename U> friend foo<U>::foo(U);
                                 ^~~~~~~~~~~
                                 typename
main.cpp:4:46: error: friends can only be classes or functions
    template <typename U> friend foo<U>::foo(U);
Run Code Online (Sandbox Code Playgroud)

我不认为foo<U>::foo是依赖类型名称,但无论如何按照建议添加类型名称会导致错误

main.cpp:4:55: error: friends can only be classes or functions
    template <typename U> friend typename foo<U>::foo(U);
Run Code Online (Sandbox Code Playgroud)

c++ clang++

7
推荐指数
1
解决办法
120
查看次数

标签 统计

c++ ×1

clang++ ×1