移动语义&返回const值 [英] Move semantics & returning const values

查看:109
本文介绍了移动语义&返回const值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个习惯(?!?!?)返回一个const值。像这样...

  struct s; 

s const make_s();

s const& s0 = make_s();
s const s1 = make_s();

使用移动操作和r值引用以及以下函数...

  void take_s(s& s0); 
void take_s(s const&& s0); //没有意义

我不能再写...

  take_s(make_s()); 

我开始使用返回常量值的约定的主要原因是为了防止有人写这样的代码...

  make_s()。mutating_member_function(); 

用例如下:

  struct c_str_proxy {
std :: string m_s;

c_str_proxy(std :: string& s):m_s(std :: move(s)){
}
};

c_str_proxy c_str(std :: string& s){
return c_str_proxy(s);
}

char const * const c_str(std :: string const& s){
return s.c_str();
}

std :: vector< std :: string> const& v = make_v();
std :: puts(c_str(boost :: join(v,,)));

std :: string const my_join(std :: vector< std :: string> const& v,char const * sep);

//下面的工作,但我想这是事故
// IT CALLS
//
// c_str(std :: string const&) ;
//
//但我认为温度返回
//
// my_join(v,;)
//
/ / IS NO LONGER ALIVE BY THE TIME我们在里面
//
// std :: puts
//
//因为我们使用c_str()OF A TEMPORARYstd :: string
//
std :: puts(c_str(my_join(v,;)));

看起来好像返回常量值和r值引用不会混合在此特定用法案件。是这样吗?

  **编辑0:额外问题... ** 

对象是临时的。为什么要const防止移动?为什么我们不能移动const临时表?

解决方案

一方面,您想要防止对返回的对象进行修改,但另一方面 ,您想要允许修改(这是一个移动操作是,它修改源对象,通过 其内部资源)。



你需要弥补你的想法。你想让对象是不可变的,还是你希望人们能够修改它?



对于什么是值得的,我真的看不到你' d增益,通过返回const临时值。是的,您可以阻止用户在其上调用变更成员函数,但为什么要?最好的情况是,能够这样做是有用的,最糟糕的是,这是一个容易避免的错误。



你所做的并不是真的很有意义。 b

右值引用和移动语义背后的整个想法是临时值是临时的,因此它们可以被修改而不会伤害任何人。


I have the habit (?!?!?) of returning everything as a "const" value. Like this...

struct s;

s const make_s();

s const &s0 = make_s();
s const s1 = make_s();

With move operations and r-value references and the following functions...

void take_s(s &&s0);
void take_s(s const &&s0);  //  Doesn't make sense

I can no longer write...

take_s(make_s());

The main reason I started using the convention of returning const values is to prevent someone from writing code like this...

make_s().mutating_member_function();

The use case is the following...

struct c_str_proxy {
    std::string m_s;

    c_str_proxy(std::string &&s) : m_s(std::move(s)) {
    }
};

c_str_proxy c_str(std::string &&s) {
    return c_str_proxy(s);
}

char const * const c_str(std::string const &s) {
    return s.c_str();
}

std::vector < std::string > const &v = make_v();
std::puts(c_str(boost::join(v, ", ")));

std::string const my_join(std::vector < std::string > const &v, char const *sep);

//  THE FOLLOWING WORKS, BUT I THINK THAT IS ACCIDENTAL
//  IT CALLS
//
//      c_str(std::string const &);
//
//  BUT I THINK THE TEMPORARY RETURNED BY
//
//      my_join(v, "; ")
//
//  IS NO LONGER ALIVE BY THE TIME WE ARE INSIDE
//
//      std::puts
//
//  AS WE ARE TAKING THE "c_str()" OF A TEMPORARY "std::string"
//
std::puts(c_str(my_join(v, "; ")));

Looks as if "returning const value" and r-value references don't mix in this particular use case. Is that right?

**Edit 0: Extra question...**

The object is temporary anyway. Why should "const" prevent moving? Why can't we move "const" temporaries?

解决方案

You have two conflicting goals. On one hand, you want to prevent modifications from being made to the returned object, but on the other hand, you want to allow modifications to be made (that's what a move operation is. It modifies the source object, by stealing its internal resources).

You need to make up your mind. Do you want the object to be immutable, or do you want people to be able to modify it?

For what it's worth, I don't really see what you'd gain by returning const temporaries in the first place. Yes, you prevent people from calling a mutating member function on it, but why would you want to? At best, being able to do so can be useful, and at worst, it's a mistake that is easily avoided.

And what you're doing doesn't really make much sense. The entire point in a temporary is that it's going to go away in a moment, so who cares if it gets modified?

The entire idea behind rvalue references and move semantics is that temporaries are temporary, and so they can be modified without harming anyone.

这篇关于移动语义&amp;返回const值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆