rvalue

    5热度

    3回答

    在以下代码: int foo(const int& f) //version 1 { int g = f; return int(foo(g)); // calls itself, turning into SO } int& foo(int& f) //version 2 { f *= -1; return f; } int main()

    21热度

    2回答

    有没有办法在C++中编写一个接受左值和右值参数的函数,而不必将其作为模板? 例如,假设我编写了一个函数print_stream,该函数从istream中读取并打印读取到屏幕上的数据或其他内容。 我认为这是合理的调用print_stream这样的: fstream file{"filename"}; print_stream(file); 以及像这样: print_stream(fstream

    4热度

    1回答

    我理解C++ 11中的转发方法。 template <class T> void foo(T &&) foo现在将接受左值和右值。 我的问题是当我超载富进一步。 考虑这个简单的代码:如果我叫foo与A <int> &(左值)对象 template <class T> class A {}; template <class T> void foo(T &&obj) {} template

    1热度

    1回答

    例如, struct A {}; struct B { B(A&& a) : mA(std::move(a)) // Is A's constructor called here? {} A&& mA; }; 是A的构造函数调用的B初始化列表?或者它就像一个由指针实现的引用?

    0热度

    1回答

    对于下面的代码,我想使用std :: move来提高效率。我有两个函数,第一个函数使用std :: move,第二个函数只是调用第一个函数。那么,我需要在函数“vector convertToString()”中再次使用std :: move吗?为什么?为什么不?谢谢。 class Entity_PortBreakMeasure { public: Entity_PortBreakMeasu

    -1热度

    1回答

    我真的很困惑他们在C++中的存在。问题在于它们部分实现了(至少在VC++ 2013中)。与错误代码 int pArray[] = new int[2]; ::例如,你不能的存储器中的动态地分配的数组分配给数组对象 cmd_c++_test___.cpp(88): error C2440: 'initializing' : cannot convert from 'int *' to 'int

    1热度

    1回答

    根据http://en.cppreference.com/w/cpp/language/move_constructor; “A类可以有多个移动构造,例如既T::T(const T&&)和T::T(T&&)” 当将一个想通过恒定的右值到移动构造函数?

    2热度

    2回答

    考虑以下内容,其中有些内容是通过多层添加到载体: class A { public: void Add(Content c) { // Considerable amount of checking code here. v.push_back(c); } private: std::vector<Content> v; }; class

    4热度

    1回答

    为什么这个代码不能编译?如果我用另一种类型的那不可复制它工作正常更换std::stringstream error: use of deleted function ‘std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)’ f(std::stringstream{}

    5热度

    2回答

    我有一个View和Shape类,其中View拥有它的Shape对象。 我正在实施这个作为unique_ptr的矢量。 在函数View :: add_shape(std :: unique_ptr & & shape)中,我仍然需要在rvalue参数上使用std :: move来编译它。为什么? (使用GCC 4.8) #include <memory> #include <vector> us