'using' 语句用 g++ 编译,用 clang 编译失败

2023-10-17C/C++开发问题
0

本文介绍了'using' 语句用 g++ 编译,用 clang 编译失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有以下结构的代码(这当然在现实中要复杂得多,尤其是Base"是一个三行,但我试图抓住它的要点):

template A类{};模板B类{上市:B(){};};模板C类:公共B<A>>{上市:使用基数=B<A>>;使用 Base::B;};static const CC{};

代码通过 g++ 编译得很好

g++ -c test.cpp -std=c++11

但是,使用 clang++ 我收到一条我不太明白的错误消息

clang++ -c test.cpp -std=c++11

<块引用>

test.cpp:14:14: 错误:依赖使用声明解析为没有typename"的类型使用 Base::B;

我的代码有什么问题吗,或者这是 clang 中的一个错误?

注意:当使用 B<A<T>>::B; 编写 时,它可以用两个编译器很好地编译,但这不是我问题的真正解决方案.

clang 版本是 3.5.0,gcc 版本是 4.9.2

解决方案

这个案例已经在 C++ 委员会中讨论过(根据 Richard Smith https://llvm.org/bugs/show_bug.cgi?id=23107#c1) 现在事情变得更清楚了:

使用 Base::B

不是旨在成为有效代码.

下面的方法是引入类别名Base时正确表达构造函数继承的方式:

使用 Base::Base

然而,clang 产生的错误消息具有误导性,有望作为此错误报告的一部分得到解决(https://llvm.org/bugs/show_bug.cgi?id=22242).

I have code of the following structure (which is of course much more complex in reality, especially "Base" is a three-liner, but I've tried to capture the gist of it):

template <class T>
class A {};

template <class T>
class B {
public:
    B(){};
};

template <class T>
class C : public B<A<T>> {
public:
    using Base = B<A<T>>;
    using Base::B;
};

static const C<int> c{};

The code compiles fine with g++ via

g++ -c test.cpp -std=c++11

However, with clang++ I get an error message I don't really understand

clang++ -c test.cpp -std=c++11

test.cpp:14:14: error: dependent using declaration resolved to type without 'typename' using Base::B;

Is there anything wrong with my code or is this a bug in clang?

Note: When writing using B<A<T>>::B; it compiles fine with both compilers, but this not a real solution to my problem.

Edit: clang version is 3.5.0, gcc version is 4.9.2

解决方案

This case has been discussed in the C++ committee (according to Richard Smith https://llvm.org/bugs/show_bug.cgi?id=23107#c1) and things have gotten clearer now:

using Base::B

is not intended to be valid code.

The following method is the correct way to express constructor inheritance when introducing a class alias Base:

using Base::Base

However, the error messages produced by clang are misleading and will hopefully be solved as part of this bug report (https://llvm.org/bugs/show_bug.cgi?id=22242).

这篇关于'using' 语句用 g++ 编译,用 clang 编译失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6