【问题标题】:MSVC's standard library does not define spaceship operator for std::string, std::shared_ptr, and so?MSVC 的标准库没有为 std::string、std::shared_ptr 等定义 spaceship 运算符?
【发布时间】:2021-05-13 10:26:21
【问题描述】:

我正在尝试使我的程序多平台,最初是为 Linux 编写的。 MSVC(我使用的是 19.28)被告知从 19.20 版(https://en.cppreference.com/w/cpp/compiler_support/20)开始支持宇宙飞船操作符,但它似乎没有为 std::string 或 std::shared_ptr 定义这个操作符(可能对于许多其他众生)。

我真正想做的是:


// int x(1), y(2); // ok!
// std::string x("1"), y("2"); // nope
std::shared_ptr<int> x(new int), y(new int); // nope
auto r = x <=> y; // error C2676 for string and shared_ptr

现场示例: https://godbolt.org/z/Eo49hh

它在 GCC 10.2 下工作。我是否在这里遗漏了一些要点,或者它并没有真正得到完全支持?

【问题讨论】:

  • 你是否在项目的Property页面中将C++语言标准设置为std:c++latest
  • @D-RAJ 是的,在 Godbolt 的实时示例中你可以看到它已经设置好了。我刚刚研究了 msvc 的标准标头,显然它只是没有定义(与 gcc 标头相反)。也许有人知道任何解决方法?

标签: c++ visual-c++ c++20 spaceship-operator


【解决方案1】:

MSVC 支持operator&lt;=&gt;,但这并不意味着它们已为所有库实现。从您到 cppreference 的链接中,在 C++20 库功能 部分下,它实际上表明 MSVC 没有 P1614R2将 添加到标准库 )

它部分支持P0768R1库支持运算符 &lt;compare&gt;)和完全支持P1185R2运算符) p>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多