相关疑难解决方法(0)

在具有参考字段的类上放置新的

这是来自 C++20 规范 ( [basic.life]/8 )的代码示例:

struct C {
  int i;
  void f();
  const C& operator=( const C& );
};

const C& C::operator=( const C& other) {
  if ( this != &other ) {
    this->~C();              // lifetime of *this ends
    new (this) C(other);     // new object of type C created
    f();                     // well-defined
  }
  return *this;
}

int main() {    
  C c1;
  C c2;
  c1 = c2;   // well-defined
  c1.f();    // well-defined; c1 refers to a new object of type …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer c++20 stdlaunder

11
推荐指数
2
解决办法
329
查看次数

从构造函数分配包含“std::string”的 ar 值时,clang 中存在明显错误

在测试const对象成员的处理时,我在 clang 中遇到了这个明显的错误。该代码可在 msvc 和 gcc 中运行。然而,该错误仅出现在非常量中,这无疑是最常见的用途。我做错了什么还是这是一个真正的错误?

https://godbolt.org/z/Gbxjo19Ez

#include <string>
#include <memory>

struct A
{
    // const std::string s; // Oddly, declaring s const compiles
    std::string s;
    constexpr A() = default;
    constexpr A(A&& rh) = default;
    constexpr A& operator=(A&& rh) noexcept
    {
        std::destroy_at(this);
        std::construct_at(this, std::move(rh));
        return *this;
    }
};

constexpr int foo()
{
    A i0{};    // call ctor
    // Fails with clang. OK msvc, gcc
    // construction of subobject of member '_M_local_buf' of union with no active member …
Run Code Online (Sandbox Code Playgroud)

c++

7
推荐指数
1
解决办法
267
查看次数

标签 统计

c++ ×2

c++20 ×1

language-lawyer ×1

stdlaunder ×1