次のコードを検討してください。
#include <vector>
#include <boost/noncopyable.hpp>
struct A : private boost::noncopyable
{
A(int num, const std::string& name)
: num(num),
name(name)
{
}
A(A&& other)
: num(other.num),
name(std::move(other.name))
{
}
int num;
std::string name;
};
std::vector<A> getVec()
{
std::vector<A> vec;
vec.emplace_back(A(3, "foo"));
// vec.emplace_back(3, "foo"); not available yet in VS10?
return vec; // error, copy ctor inaccessible
}
int main ( int argc, char* argv[] )
{
// should call std::vector::vector(std::vector&& other)
std::vector<A> vec = getVec();
return 0;
}
これは VS2010 ではコンパイルされません。これは明らかにA
コピーできないためです。したがって、関数から a を返すことはできません。noncopyable
std::vector<A>
std::vector<A>
しかし、RVO の概念を考えると、この種のことが不可能であるというのは、私には正しくありません。ここで戻り値の最適化を適用すると、コピーの作成を省略でき、 への呼び出しgetVec()
が有効になります。
では、これを行う適切な方法は何でしょうか? これはVS2010 / C++ 11でまったく可能ですか?