我想知道有一个方法返回一个r值.具体来说,我想知道是否有办法用重载运算符来做到这一点.我有这个代码:
struct vec4 {
float x;
float y;
float z;
float w;
...
inline float operator [] (int i)
{
switch (i) {
case 0:
return this->x;
case 1:
return this->y;
case 2:
return this->z;
case 3:
return this->w;
default:
exit(1);
return 0;
}
}
};
Run Code Online (Sandbox Code Playgroud)
我怎样才能改变这一点,以便我可以使用某些东西来达到效果
vec4 v;
...
v[2] = 5.0f;
Run Code Online (Sandbox Code Playgroud)
我听说过C++ 11中的rvalue引用,它们可能是一个潜在的解决方案吗?
编辑:我找到了一种方法来输入我的实际代码.
有没有办法删除所有函数的'plumb'版本,而无需将'hit'行更改为'fixed'?
是的,我的程序工作正常,但我想如果有办法从我的所有功能的版本中获取.
请记住,这int不是int我的程序,而是一个类型别名,可以是对象(例如container_reference<std::array<double,4>>)或引用(例如std::array<double,4> &)
void func(int &&m) { cout << "rvalue: " << m << endl; }
void func(int &m) { cout << "lvalue: "; func(std::move(m)); } // PLUMB!
int main()
{
int a = 5;
func(a); // HIT!
func(std::move(a)); // FIXED!
func(6);
func(a + 5);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试移动语义,我想知道如果右值引用超出范围会发生什么。使用以下代码,如果我将std :: move左值移入,则会遇到运行时问题
function(T t) with t = std::move(lvalue) --> SEGFAULT OR double free
Run Code Online (Sandbox Code Playgroud)
但不成
function(T &&t) with t = std::move(lvalue) --> OK
Run Code Online (Sandbox Code Playgroud)
有人知道为什么吗?
另外,如果交换main()中的两个代码块,则会得到不同的运行时错误0_o
// Compile with:
// g++ move_mini.cpp -std=c++11 -o move_mini
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <utility>
using namespace std;
int num_copied;
class T{
public:
T() : a(nullptr), b(nullptr){};
T(const T &t) : a(new string(*t.a)),
b(new string(*t.b)){
num_copied++;
};
T(T &&t){
*this = move(t);
};
T(string s1, string s2){
this->a = …Run Code Online (Sandbox Code Playgroud) 这是我的意思的一个简短的例子.我发现很难简洁地说出这个问题.
struct A {};
struct B : public A {};
void foo(A*&& ap)
{
ap = nullptr;
}
int main()
{
B b;
A* ap = &b;
foo(std::move(ap));
std::cout << (ap == nullptr ? "null" : "not null") << std::endl;
B* bp = &b;
foo(std::move(bp));
std::cout << (bp == nullptr ? "null" : "not null") << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我希望这会打印出来
null
null
Run Code Online (Sandbox Code Playgroud)
而是它打印
null
not null
Run Code Online (Sandbox Code Playgroud)
快速查看x86反汇编会显示不需要的复制,仅适用于以下B*情况:
lea eax,[bp]
push eax
call std::move<B * &> (03E14BFh)
add esp,4
mov …Run Code Online (Sandbox Code Playgroud) 我正在阅读一篇关于Thomas Becker的 c ++ 11 rvalue引用的博客,页面底部附近的以下语法让我感到困惑.
int& foo();
foo() = 42; // ok, foo() is an lvalue
Run Code Online (Sandbox Code Playgroud)
到底是foo什么?一个返回a的函数指针int; 一个东西?
如果是一个函数对象,你如何为它赋值,为什么是foo左值?
我对以下代码感到困惑:
#include <iostream>
int main()
{
int x{};
int&& rvx = static_cast<int&&>(x);
++rvx;
std::cout << x << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
它的输出是1.我不明白这是如何工作的.所述static_cast应该左值铸造x成一个x值,然后将其分配给rvx.为什么递增rvx会导致变化x?这是因为转换后的左值到右值基本上位于相同的内存位置,但它现在只被视为右值?我的印象(这可能是假的),不知何故,演员创造了一个临时的参数.
请考虑以下代码:
#include <iostream>
template <class T>
class value_wrapper
{
public:
value_wrapper(T& pv) : v(pv) { std::cout<< "CONS.REF:" << pv << " AT:" << (void*)this << std::endl; }
value_wrapper(T&& pv) : v(pv) { std::cout<< "CONS.UNIREF:" << pv << " AT:" << (void*)this << std::endl; }
virtual ~value_wrapper() { std::cout<< "DEST:" << v << " AT:" << (void*)this << std::endl; }
value_wrapper(const value_wrapper& ov) : v(ov.v) { std::cout<< "CONS.COPY.REF:" << v << " AT:" << (void*)this << std::endl; }
value_wrapper(value_wrapper&& ov) : …Run Code Online (Sandbox Code Playgroud) 我知道这可以用来执行完美的转发:
template <typename A>
void foo(A&&) { /* */ }
Run Code Online (Sandbox Code Playgroud)
这可以用于在某种类型上执行完美转发:
template <typename A, std::enable_if_t<std::is_same<std::decay_t<A>, int>::value, int> = 0>
void foo(A&&) { /* */ }
Run Code Online (Sandbox Code Playgroud)
但这些只是函数的模板,这意味着,这些函数会扩展为某些函数,然后将这些函数用于可能使用它的每个特殊情况.但是,这些扩展到:
void foo(A&) 和 void foo(A&&)
要么
void foo(A&) 和 void foo(A)
我一直认为,这将是第一个,但后来我注意到,在那种情况下,你将无法A const用作函数的参数,这当然有效.
然而,如果您使用正常的非常数左值,则第二个将是不明确的.它会打电话foo(A&)还是foo(A)?
c++ templates rvalue-reference move-semantics perfect-forwarding
constC++中函数参数的修饰符表示该函数不能改变参数值,但不保证在函数执行过程中不能被其他人改变。因此,编译器无法根据数据不变性进行任何优化。
据我了解,右值引用意味着给定的对象是临时的,因此其他人都无法访问其 data。在这种情况下,编译器可以进行积极的优化吗?
它将允许通过某种方式获得更快的代码
template<class T>
class Immutable
{
private:
const T val;
public:
operator const T && () { return std::move(val); }
};
Run Code Online (Sandbox Code Playgroud)
(只是示例代码),或者const&&当我们确定它们在函数调用期间无法更改时传递值。是否有可能,或者有一些未提及的问题?
c++ optimization constants compiler-optimization rvalue-reference
我想在下面的例子中更好地理解运算符&&的使用 std::string
码:
#include <iostream>
using namespace std;
const std::string GetEditTxtAccount()
{
std::string str = "Hello";
return str;
}
int main()
{
const string&& x = GetEditTxtAccount();
^^^
}
Run Code Online (Sandbox Code Playgroud)
那么为什么我们&&在主要使用运营商呢?
谢谢.
c++ ×10
rvalue-reference ×10
c++11 ×5
c++14 ×1
constants ×1
lvalue ×1
optimization ×1
pointers ×1
rvalue ×1
templates ×1