rvalue函数重载 [英] rvalue function overloading
问题描述
我想重载一个函数,以便它以某种方式处理其参数,然后返回对参数的引用 - 但如果参数不可变,则它应返回一个操作的 copy 参数。
I want to overload a function so that it manipulates its argument in some way and then returns a reference to the argument – but if the argument is not mutable, then it should return a manipulated copy of the argument instead. After messing around with it for ages, here's what I've come up with.
using namespace std;
string& foo(string &in)
{
in.insert(0, "hello ");
return in;
}
string foo(string &&in)
{
return move(foo(in));
}
string foo(const string& in)
{
return foo(string(in));
}
此代码似乎工作正常,但我有兴趣听到,如果有人
This code seem to work correctly, but I'm interested to hear if anyone can think of a better way to do it.
这是一个测试程序:
int main(void)
{
string var = "world";
const string var2 = "const world";
cout << foo(var) << endl;
cout << var << endl;
cout << foo(var2) << endl;
cout << var2 << endl;
cout << foo(var + " and " + var2) << endl;
return 0;
}
正确的输出是
hello world
hello world
hello const world
const world
hello hello world and const world
我认为如果我能做到这一点会更加轻松:
I figure it would be slightly neater if I could do this:
string& foo(string &in)
{
in.insert(0, "hello ");
return in;
}
string foo(string in)
{
return move(foo(in));
}
当然,这不工作,因为大多数函数调用 foo
将是不明确的 - 包括在 foo
本身的调用!但如果我能以某种方式告诉编译器优先第一个...
Of course, that doesn't work because most function calls to foo
would be ambiguous – including the call in foo
itself! But if I could somehow tell the compiler to prioritize the first one...
正如我所说,我发布的代码工作正常。我不喜欢的主要是它是重复的额外代码。如果我有一堆函数,它会变得相当混乱,大多数是非常重复的。所以作为第二部分,我的问题:任何人都可以想到一种方法自动生成的第二个和第三个 foo
函数的代码?例如
As I said, the code I posted works correctly. The main thing I don't like about it is the repetitive extra code. If I had a bunch of functions like that it would become quite a mess, and most of it would be very repetitive. So as a second part to my question: can anyone think of a way to automatically generate the code for the second and third foo
functions? eg
// implementation of magic_function_overload_generator
// ???
string& foo(string &in);
magic_function_overload_generator<foo>;
string& bar(string &in);
magic_function_overload_generator<bar>;
// etc
推荐答案
我会把所有的引用都删除,只是写一个函数传递并返回值:
I would get rid of the references all together and just write one function that passes and returns by value:
std::string foo(std::string in)
{
in.insert(0, "hello ");
return in;
}
如果传递一个左值,则将复制输入字符串。如果你传递一个右值,它将被移动。
If you pass an lvalue, the input string will be copied. If you pass an rvalue, it will be moved.
离开函数时,返回值优化可能会进入,因此返回基本上是一个无操作。如果编译器决定,则结果将被移动(即使中的是一个左值)。
When leaving the function, named return value optimization will probably kick in, so the return is basically a no-op. If the compiler decides against that, the result will be moved (even though in
is an lvalue).
关于右值引用的好处在于,您必须更少地考虑在用户代码中放置引用的位置以提高效率。对于可移动类型,pass-by-value实际上与其获得的效率一样高效。
The good thing about rvalue references is that you have to think less about where to put references in user code to gain efficiency. With movable types, pass-by-value is practically as efficient as it gets.
这篇关于rvalue函数重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!