rvalue

    0热度

    1回答

    我已经使TestClass更好地说明了问题。 正如你所看到的,当对象被压入vector并且使用std::move函数初始化构造函数时,会调用move构造函数。 但是,当我们调用TestClass testvar2(rvalue_func());你可以看到底,该rvalue_func()的结果值分配给testvar2对象,但此举构造不叫,也没有其他的构造函数或赋值操作符被称为... 问题: 它是如何

    6热度

    2回答

    因此我知道s2绑定到表达式s1 + s1,但是在s2被分配的时候评估过,还是懒惰,并在调用s2 += "Test";时被评估?并且s2也会为内存暂存一个字符串? #include <iostream> #include <string> int main() { std::string s1 = "Test"; std::string&& s2 = s1 + s1;

    1热度

    2回答

    我已经做了简单的测试以了解更好的移动语义。输出结果对我而言是意外的。贝娄是我的测试功能: template<class T> void test(T&& v) { v++; } void main() { int v = 1; test(std::move(v)); std::cout << "Output:" << v << std::endl

    1热度

    2回答

    class Vec3{ private: float x, y, z; public: Vec3() = default; Vec3(const float c) {x = c; y = c; z = c;} static Vec3& normalize(Vec3& v) {/* normalize */ return v;} }; Vec

    3热度

    4回答

    我刚开始使用C++ 11 r值。我读了一些教程,但我还没有找到答案。 设置类变量的最佳方式(最有效的方法)是什么?下面的代码是否正确? (让我们假设std :: string已经定义了移动构造函数和赋值运算符)。 class StringWrapper { private: std::string str_; public: StringWrapper() : str_

    -1热度

    1回答

    最近我无意中程序如下: void someFunction(const SomeClass&& value){ //Some Code } 我不是故意的“常量”关键字添加到值参数。但是,这让我想:如果不存在const的等价函数存在,是否甚至有可能以某种方式调用此函数:?如果是的话,这是否有意义,是否有一些实际用途呢?

    2热度

    2回答

    为什么下面的代码... #include <iostream> #include <map> template< typename T, typename U > class Map { public: Map(const T& t, const U& u) { map_[ t ] = u; } Map< T, U >& operator() (const T& t

    13热度

    1回答

    我试图理解C++ 11中的左值和右值。所以我写了一个测试代码: int x = 10; int foo() { return x; } int& bar() { return x; } int&& baz() { return 10; } int main() { int& lr1 = 10; // error: lvalue references rvalue i

    3热度

    2回答

    我有一个类,它包含一个用unique_ptr管理的c-style数组。我想提供一个构造函数: class A { unique_ptr<T[]> p; public: A(int d, X x) : p(new T[d]) { //Transfer from x to p without copying } } 这样我就可以建立我的对象的东西,如:

    0热度

    1回答

    我目前正在通过'Effective Modern C++'来更新我对该语言的了解,并刚刚完成了第27项,该项处理重载函数,它将转发(或通用本书称他们)参考。使用下面的代码,我写道: #include <iostream> template <typename T> void func(T&& param) { std::cout << "forwarding reference v