英文:
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 <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;
}
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("-stdlib=libc++")
add_executable(three_way_comparison main.cpp)
It fails with
> /home/user/CLionProjects/three-way-comparison/main.cpp:6:10: warning:
> explicitly defaulted three-way comparison operator is implicitly
> deleted [-Wdefaulted-function-deleted]
> auto operator<=>(const foo&) const = default;
> ^ /home/user/CLionProjects/three-way-comparison/main.cpp:5:17: note:
> defaulted 'operator<=>' is implicitly deleted because there is no
> viable three-way comparison function for member 'member'
> std::string member{"Hello, Kitty!"};
> ^ /usr/lib/llvm-14/bin/../include/c++/v1/__utility/pair.h:340:1: note:
> candidate template ignored: could not match 'pair' against
> 'basic_string' yada, yada
the /usr/lib/llvm-14/include/c++/v1/string
header indeed does not have the operator<=>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论