On 24 Jun., 17:23, Sousuke wrote:
The optimizations that rvalue references make possible are nice, but
I'm having one problem with them (rvalue refs): they sometimes lead to
too much verbosity.
When defining a constructor or a setter, you usually take a const T&
and assign it to one of the class's members. In C++0x, you can
additionally define an overload that takes a T&&:
class OneString
{
public:
OneString(const string& s) : m_s(s)
{}
OneString(string&& s) : m_s(move(s))
{}
private:
string m_s;
};
One additional overload is not too much verbosity, but see the two-
argument case:
In this particular instance (constructor, the members don't yet exist)
I'd simply take the strings by value. This also scales well for
multiple string parameters:
class ThreeStrings
{
public:
ThreeStrings(string s1, string s2, string s3)
: m_s1(move(s1)), m_s2(move(s2)), m_s3(move(s3))
{}
private:
string m_s1;
string m_s2;
string m_s3;
};
This works perfectly because we know that std::string supports quick
moving and because m_s1, m_s2 and m_s3 don't yet exist:
argument was #copies #moves
---------------------------------------------
lvalue 1 1
rvalue 0 2 (or 1 if elided)
=> no unnecessary copies
You could also use pass-by-value for a "setter" method:
void setter_I(string s1, string s2, string s3)
{
m_s1 = move(s1);
m_s2 = move(s2);
m_s3 = move(s3);
}
or pass-by-ref-to-const:
void setter_II(string const& s1, string const& s2, string const& s3)
{
m_s1 = s1;
m_s2 = s2;
m_s3 = s3;
}
But in both cases this might involve some unnecessary copying. In the
first case one string member object might already have enough
resources allocated to hold the new string value. In the second case
you'll have unnecessary copies when the argument was an rvalue.
If you don't want this, the only feasible solution I know of (perfect
forwarding) looks like this:
#include <string>
#include <type_traits>
#include <utility>
#define REQUIRES(...) class=typename \
std::enable_if<(__VAR_ARGS__)>::type
[...]
class ThreeStrings {
[...]
template <class S1, class S2, class S3,
REQUIRES( std::is_convertible<S1,std::string>::value
&& std::is_convertible<S2,std::string>::value
&& std::is_convertible<S3,std::string>::value )>
void setter_III(S1 && s1, S2 && s2, S3 && s3)
{
m_s1 = std::forward<S1>(s1);
m_s2 = std::forward<S2>(s2);
m_s3 = std::forward<S3>(s3);
}
[...]
};
whether to use pass-by-value or perfect forwarding. Either way, it's
better than what we had before.