首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >朋友缩写模板函数- clang和gcc不同

朋友缩写模板函数- clang和gcc不同
EN

Stack Overflow用户
提问于 2020-07-07 16:09:30
回答 1查看 125关注 0票数 5

以下代码用clang编译,而不是用gcc编译

代码语言:javascript
复制
template<typename T>
class number {
    T num;
public:
    number(T num = 0): num(num) {}
    
    friend auto add(auto a, auto b);
};

auto add(auto a, auto b) {
    // the decltype(a) is needed to make clang happy
    // see: https://stackoverflow.com/questions/62779242
    return number<decltype(a)>{a}.num + number<decltype(b)>{b}.num;
}

int main() {
    auto result = add(1.0, 2.0);
}

gcc提供的编译错误(带有-std=c++20的10.1版):

代码语言:javascript
复制
In instantiation of 'auto add(auto:13, auto:14) [with auto:13 = double; auto:14 = double]':
error: 'double number<double>::num' is private within this context
   return number<decltype(a)>{a}.num + number<decltype(b)>{b}.num;

假设这是bug gcc是合理的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-12 11:40:06

显然,这是GCC 10.1.0的一个错误,在正确工作到9.3之后。

漏洞在GCC 10.3中被打开和修复。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62779488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档