小编son*_*yao的帖子

为什么在我的代码中调用复制构造函数而不是移动构造函数?

我试图了解移动构造函数和右值引用。所以我在https://www.onlinegdb.com/online_c++_compiler上尝试了此代码。但是结果使我感到困惑。

#include <iostream>
#include <type_traits>


class A {
public:
  A() { std::cout << "Constructed" << std::endl; }
  A(const A& )= delete;
  A(A&&) { std::cout << "Move Constructed" << std::endl; }
};

int
main ()
{
  A&& a = A();
  A b = a; // error: use of deleted function ‘A::A(const A&)’
  //A b = static_cast<decltype(a)>(a); // This works, WTF?
  std::cout << std::is_rvalue_reference<decltype(a)>::value << std::endl; // pretty sure a is rvalue reference.

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ rvalue-reference c++11

2
推荐指数
1
解决办法
67
查看次数

C++函数什么时候删除返回值?

我有以下 C++ 代码。

string getName()
{
    return "C++";
}

void printName(const char* name)
{
    cout << name << endl;
}

int main()
{
    printName(getName().c_str());
}
Run Code Online (Sandbox Code Playgroud)

该函数getName返回一个string. 我将函数c_str的指针传递给函数。我想知道在调用函数之前返回的内容是否会被删除。如果不是那么什么时候删除返回值。stringprintNamestringprintName()

c++ string temporary return-value lifetime

2
推荐指数
1
解决办法
1055
查看次数

std :: string :: substr返回的对象的生存期

我需要知道以下内容通常是否有效。

string s = "some value";
string v = s.substr(0, 50).c_str();
Run Code Online (Sandbox Code Playgroud)

分配v始终有效吗?由于所传回的物件的生存期会不会出现任何问题substr()

c++ string temporary lifetime c++11

2
推荐指数
1
解决办法
85
查看次数

带有空指针的二维指针数组 C++

如何在 C++ 中声明一个二维指针数组并用空指针初始化它?

我试图这样做

int *arr[20][30]= nullptr;
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers initialization

2
推荐指数
1
解决办法
374
查看次数

将 unsigned int 的最大值转换为 int 并期望结果为 -1 是否可移植?

我找到了一个 C++ 代码,我们将初始化为最大值的 unsigned int 32 转换为有符号 int 并期望它是-1. 它在经过测试的编译器上运行良好,但它真的可移植吗?

int GetBusinessDataID()
{
    u32_t id = ~0;

    // Some code that may return a valid ID.

    return id; // Here we expect to return -1.
}
Run Code Online (Sandbox Code Playgroud)

c++ unsigned implicit-conversion

2
推荐指数
1
解决办法
82
查看次数

C++ 访问静态 constexpr 数组

我试图从另一个类中声明的数组中获取一些值。该数组具有固定长度和常量元素(我将 100% 从不修改其值,所以这就是我将其设为常量的原因)。

但是,当我尝试访问main函数中的第一个元素时,出现编译错误:

basavyr@Roberts-MacBook-Pro src % g++ -std=c++11 main.cc
Undefined symbols for architecture x86_64:
  "Vectors::vec1", referenced from:
      _main in main-c29f22.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在使用 clang(最新版本)在 macOS Catalina 上进行编译。

[问]:可能是什么问题?先感谢您。

这是代码:

#include <iostream>

class Dimension
{
public:
    static constexpr int dim1 = 2;
    static constexpr int dim2 = 1;
};

class Vectors
{
public:
    static constexpr double vec1[2] = {4.20, 6.9};
};

int main()
{ …
Run Code Online (Sandbox Code Playgroud)

c++ class constexpr c++11

2
推荐指数
1
解决办法
491
查看次数

c++ std::list::front() 返回空列表是什么?

如果我有代码:

struct Test
{
  int x = 10;
};

int main()
{
  std::list<Test> linkedList;
  std::cout << linkedList.front().x << std::endl;
}
---

out -> 0
Run Code Online (Sandbox Code Playgroud)

为什么我的 test.x 值是 0?如果我将列表更改为 int 类型,它返回 0。如果我给它一个 char 类型,我什么也得不到(或“”)。

我很好奇它是如何(以及为什么)发生在引擎盖下的。它如何处理返回任何类型的值而不退出程序或需要尝试/捕获?

c++ null stdlist

2
推荐指数
1
解决办法
596
查看次数

为什么 std::move 不会在默认移动构造函数中将源变量更改为默认值?

我试图理解移动构造函数。

我在类的构造函数中分配内存并在析构函数中销毁它。

当我尝试移动类时,我仍然有一个双空闲。

#include <algorithm>

class TestClass
{
 public:
  TestClass() {a_ = new int[1];}
  TestClass(TestClass const& other) = delete;
  TestClass(TestClass && other) noexcept // = default;
  {
    this->a_ = std::move(other.a_);
  }
  ~TestClass() {delete[] a_;}
 private:
  int* a_ = nullptr;
};

int main( int argc, char** argv )
{
  TestClass t;
  TestClass t2 = std::move(t);
}
Run Code Online (Sandbox Code Playgroud)

为什么std::move不改为 nullptr other.a_

如果移动构造函数是默认的,我也有同样的问题。

我发现了以下问题,但我仍然不知道为什么移动运算符不将源变量更改为默认值。

std::move 如何使原始变量的值无效?

C++如何将对象移动到nullptr

C++ std::move 一个指针

c++ move-semantics c++11 stdmove

2
推荐指数
1
解决办法
140
查看次数

如果返回类型从 auto 更改为 bool,则 Lambda 函数会抛出错误

我在 C++ 中练习 lambda 函数,下面的代码工作正常


void insertionSort(int* a, int size, bool reverse=false) {
    auto comp = [](int a, int b, bool reverse) {
        return reverse ? a > b : b < a;
    };
    
    for (int i = 0; i < size; i++) {
        int current = a[i];
        cout << current <<endl;
        int j = i-1;
        while (j >= 0 && comp(current, a[j], reverse)) {
           a[j+1] = a[j]; //shift right
           j--;
        }
        a[j+1] = current;
    }
    show(a, size); //another function …
Run Code Online (Sandbox Code Playgroud)

c++ lambda auto c++11

2
推荐指数
1
解决办法
77
查看次数

为什么结构内部的对象初始化不同?

我创建了一个类Point,这里是对应的hpp文件。

#ifndef POINT
#define POINT
class Point
{
 protected:
 int x;
 int y;
 public:
 Point(int x = 10, int y = 10);
 void movePoint(int moveX, int moveY);
 void printCoordinates();
};
#endif
Run Code Online (Sandbox Code Playgroud)

我注意到在 main 中,我可以声明一个对象并以这种方式初始化它:

Point myPoint(1, 1);
Run Code Online (Sandbox Code Playgroud)

如果我想创建一个包含两个点的结构,它不会让我以这种方式初始化它,相反,我必须使用大括号,这样:

struct segment
{
 Point point1 = {0, 0};
 Point point2 = {15, 15};
};
Run Code Online (Sandbox Code Playgroud)

这是为什么?

c++ struct initialization member-initialization c++11

2
推荐指数
1
解决办法
64
查看次数