英文:
clang fails to compile with boost::rational
问题
以下是您提供的代码的中文翻译部分:
#include <boost/rational.hpp>
#include <iostream>
int main() {
auto r = boost::rational<int>{3,2};
std::cout << r << "\n";
}
使用以下命令编译,g++ -std=gnu++2b -o try try.cpp
可以正常编译,并输出预期的 3/2
。然而,使用 clang 编译时出现以下错误:
$ clang -std=gnu++2b -o try try.cpp
In file included from try.cpp:1:
In file included from /usr/include/boost/rational.hpp:69:
In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/iomanip:42:
In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/ios_base.h:41:
In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/locale_classes.h:40:
In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/string:48:
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/stl_iterator.h:2618:35: error: missing 'typename' prior to dependent type name 'iterator_traits<_It>::iterator_category'
{ using iterator_category = iterator_traits<_It>::iterator_category; };
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated
这是在 clang 版本 15.0.7 和 gcc 版本 13.1.1 上的情况。根据 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100900 中的信息,类似的问题曾在几年前报告过,但现在已经修复。请问我做错了什么?
编辑:Boost 库版本为 1.81.0-6,安装在 Arch Linux 上。
英文:
The following code
#include <boost/rational.hpp>
#include <iostream>
int main() {
auto r = boost::rational<int>{3,2};
std::cout << r << "\n";
}
Compiles fine with the following g++ -std=gnu++2b -o try try.cpp
and outputs 3/2
as expected. However with clang I get this
In file included from try.cpp:1:
In file included from /usr/include/boost/rational.hpp:69:
In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/iomanip:42:
In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/ios_base.h:41:
In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/locale_classes.h:40:
In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/string:48:
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.1.1/../../../../include/c++/13.1.1/bits/stl_iterator.h:2618:35: error: missing 'typename' prior to dependent type name 'iterator_traits<_It>::iterator_category'
{ using iterator_category = iterator_traits<_It>::iterator_category; };
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated
This is with clang version 15.0.7 and gcc version 13.1.1. I see from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100900 that similar issues were reported a couple of years ago but are fixed now. What am I doing wrong?
Edit: Boost libraries are 1.81.0-6 installed in Arch linux.
答案1
得分: 2
更新至Clang 16。Cppreference表示C++20中的“使typename更加可选”已在Clang 16中实现,并且libstdc++ 13似乎依赖于它。
英文:
Update to Clang 16. Cppreference says that "Make typename more optional" from C++20 was implemented in Clang 16, and libstdc++ 13 seems to rely on it.
答案2
得分: 0
链接的错误修复是通过更改 libstdc++
以使其与 clang
编译器一起工作来修复的,因为 clang
似乎需要在 using
声明中使用可选的 typename
。
它没有修复 clang
本身,因此,如果另一个库(或任何有效的代码片段)也省略了 typename
,clang
仍然会报错。
需要在 clang
上提出一个错误,这里需要的是(除非已经有一个后续版本修复了它,当然(1))。这将解决这个问题的所有可能出现的情况,而不必修补多个(完全有效的)代码片段以解决 clang
的不足。
(1) 在提出错误之前,您应该尝试 clang
16,看看它是否仍然存在问题。尽管发布说明似乎只指出了对 using enum
的修复,但也可能已经在那里进行了未公告的工作。
英文:
That bug fix you link to was fixed by changing libstdc++
so that it works with the clang
compiler, because clang
seemed to require the optional typename
in the using
declaration.
It did not fix clang
itself so, if another library (or any piece of valid code) also leaves off the typename
, clang
will still complain.
A bug raised on clang
is what's needed here (unless there's already a later release that fixes it, of course<sup>(1)</sup>). This will solve all possible occurrences of this issue rather than having to patch multiple (perfectly valid) bits of code to work around a clang
deficiency.
<sup>(1)</sup> Before raising a bug, you should try clang
16 to see if it's still an issue. Although the release notes seem to indicate fixes for only using enum
, there may have been unannounced work done there as well.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论