libc++版本支持STL类型的三路比较是从版本11开始的。

huangapple go评论103阅读模式
英文:

What libc++ version supports three way comparison for STL types

问题

我正在尝试使用C++20支持、clang 14/15和libc++ 14/15编译简单的代码。以下是您提供的代码的翻译:

#include <iostream>
#include <string>

struct foo {
    std::string member{"Hello, Kitty!"};
    auto operator<=>(const foo&) const = default;
};

int main() {
    foo f;
    std::cout << f.member << std::endl;
    return 0;
}

以下是您提供的CMakeList的翻译:

cmake_minimum_required(VERSION 3.25)
project(three_way_comparison)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
add_compile_options("-stdlib=libc++")

add_executable(three_way_comparison main.cpp)

至于错误信息,这是您收到的错误翻译:

/home/user/CLionProjects/three-way-comparison/main.cpp:6:10: 警告:
显式默认的三向比较运算符被隐式删除 [-Wdefaulted-function-deleted]
    auto operator<=>(const foo&) const = default;
         ^
/home/user/CLionProjects/three-way-comparison/main.cpp:5:17: 注意:
默认的'operator<=>'被隐式删除,因为没有适用于成员'member'的可行三向比较函数
    std::string member{"Hello, Kitty!"};
                ^
/usr/lib/llvm-14/bin/../include/c++/v1/__utility/pair.h:340:1: 注意:
候选模板被忽略: 无法匹配'pair'和'basic_string'之类的
候选模板被忽略: 无法匹配'pair'和'basic_string'之类的

至于您的问题,看起来您尝试使用libc++的C++20支持,但是在标准库中找不到默认的三向比较运算符。您提到尝试安装libc++-14-dev,但似乎没有成功。

可能的解决方法之一是确保您的clang版本与libc++版本兼容。您可以尝试更新clang和libc++到最新版本,以确保它们支持C++20。

请注意,这只是可能的解决方法之一,具体取决于您的操作系统和开发环境设置。您可能还需要查看操作系统包管理器或其他工具,以确保正确安装了所需的库和工具链版本。

英文:

I'm trying to compile simple code with C++20 support, clang 14/15 and libc++ 14/15
The code

#include &lt;iostream&gt;
#include &lt;string&gt;

struct foo {
    std::string member{&quot;Hello, Kitty!&quot;};
    auto operator&lt;=&gt;(const foo&amp;) const = default;
};
int main() {
    foo f;
    std::cout &lt;&lt; f.member &lt;&lt; std::endl;
    return 0;
}

The CMakeList

cmake_minimum_required(VERSION 3.25)
project(three_way_comparison)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
add_compile_options(&quot;-stdlib=libc++&quot;)

add_executable(three_way_comparison main.cpp)

It fails with

&gt; /home/user/CLionProjects/three-way-comparison/main.cpp:6:10: warning:
&gt; explicitly defaulted three-way comparison operator is implicitly
&gt; deleted [-Wdefaulted-function-deleted]
&gt;     auto operator&lt;=&gt;(const foo&amp;) const = default;
&gt;          ^ /home/user/CLionProjects/three-way-comparison/main.cpp:5:17: note:
&gt; defaulted &#39;operator&lt;=&gt;&#39; is implicitly deleted because there is no
&gt; viable three-way comparison function for member &#39;member&#39;
&gt;     std::string member{&quot;Hello, Kitty!&quot;};
&gt;                 ^ /usr/lib/llvm-14/bin/../include/c++/v1/__utility/pair.h:340:1: note:
&gt; candidate template ignored: could not match &#39;pair&#39; against
&gt; &#39;basic_string&#39; yada, yada

the /usr/lib/llvm-14/include/c++/v1/string header indeed does not have the operator&lt;=&gt; in it. tried to run sudo apt to install libc++-14-dev, it didnt work. What I'm doing wrong? version 15 ends up with the same error.

答案1

得分: 2

三向比较支持哪个libc++版本用于STL类型?

与clang++ 16一起提供的版本。

演示

英文:

> What libc++ version support three way comparison for STL types

The one that comes with clang++ 16.

Demo

huangapple
  • 本文由 发表于 2023年7月23日 20:58:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定