微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

未为 std::string 定义三向比较运算符 (<=>)

如何解决未为 std::string 定义三向比较运算符 (<=>)

我正在尝试在我的程序中使用 c++20 的新三向运算符 () 功能,但该运算符似乎没有为字符串定义,因为这个程序

(node:11276) UnhandledPromiseRejectionWarning: Error: Evaluation Failed: TypeError: Cannot read property 'innerHTML' of null
    at __puppeteer_evaluation_script__:2:66
    at ExecutionContext._evaluateInternal (c:\Users\grung\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221:19)
    at processticksAndRejections (internal/process/task_queues.js:93:5)
    at async ExecutionContext.evaluate (c:\Users\grung\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:110:16)
    at async c:\Users\grung\javaScriptPractice\jsPractice\scrapers\amazonScrapers\imageScraper\scraper.js:32:21
(Use `node --trace-warnings ...` to show where the warning was created)
(node:11276) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:11276) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.```

产生这个错误

#include <compare>
#include <string>

using namespace std;

int main()
{

    string a = "a";
    string b = "b";
    auto x = (a <=> b);

    return 0;
}

但是,根据cppreference,这个操作应该在c++20中定义:https://en.cppreference.com/w/cpp/string/basic_string/operator_cmp

三向比较运算符适用于原始类型,因为将前两个变量更改为 int 编译没有问题,另外认实现的三向比较运算符在类中正常工作,但仅当这些类仅包含原始类型时.同样的问题也发生在 std::vector 上,据我所知,它应该支持三向比较运算符。

我正在使用 cmake,据我所知,我已将标准正确设置为 C++ 20

Error   C2676   binary '<=>': 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator

我正在 Visualstudio 中使用 MSVC 进行编译

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。