如果你想避免复制,那么我想容器必须自己创建存储的实例。
如果要调用默认构造函数,那应该没问题。只需调用 Container 的默认构造函数即可。
如果您想调用包含类型的非默认构造函数,则问题可能更大。C++0x 将为此提供更好的解决方案。
作为练习,容器可以接受一个 T,或者一个包含 T 的构造函数参数的对象。这仍然依赖于 RVO(返回值优化)。
template <class T1>
class construct_with_1
{
T1 _1;
public:
construct_with_1(const T1& t1): _1(t1) {}
template <class U>
U construct() const { return U(_1); }
};
template <class T1, class T2>
class construct_with_2
{
T1 _1;
T2 _2;
public:
construct_with_2(const T1& t1, const T2& t2): _1(t1), _2(t2) {}
template <class U>
U construct() const { return U(_1, _2); }
};
//etc for other arities
template <class T1>
construct_with_1<T1> construct_with(const T1& t1)
{
return construct_with_1<T1>(t1);
}
template <class T1, class T2>
construct_with_2<T1, T2> construct_with(const T1& t1, const T2& t2)
{
return construct_with_2<T1, T2>(t1, t2);
}
//etc
template <class T>
T construct(const T& source) { return source; }
template <class T, class T1>
T construct(const construct_with_1<T1>& args)
{
return args.template construct<T>();
}
template <class T, class T1, class T2>
T construct(const construct_with_2<T1, T2>& args)
{
return args.template construct<T>();
}
template <class T>
class Container
{
public:
T first, second;
template <class T1, class T2>
Container(const T1& a = T1(), const T2& b = T2()) :
first(construct<T>(a)), second(construct<T>(b)) {}
};
#include <iostream>
class Test
{
int n;
double d;
public:
Test(int a, double b = 0.0): n(a), d(b) { std::cout << "Test(" << a << ", " << b << ")\n"; }
Test(const Test& x): n(x.n), d(x.d) { std::cout << "Test(const Test&)\n"; }
void foo() const { std::cout << "Test.foo(" << n << ", " << d << ")\n"; }
};
int main()
{
Test test(4, 3.14);
Container<Test> a(construct_with(1), test); //first constructed internally, second copied
a.first.foo();
a.second.foo();
}