我的代码利用了一些肮脏的技巧来使它看起来像我认为的一个不错的界面。最重要的类 ,Worker
旨在让调用者用临时对象构造它;然后构造函数将接受它们。我认为代码是不言自明的:
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
struct Base
{
virtual void
print
( void )
{
cout << "Base" << endl;
};
};
struct Derived1 : public Base
{
virtual void
print
( void )
{
cout << "Derived1" << endl;
};
};
struct Derived2 : public Base
{
virtual void
print
( void )
{
cout << "Derived2" << endl;
};
};
class Worker
{
private:
/* Arrays can't hold references, and
* vectors are homogenous, so the
* only option is to use (smart) pointers. */
vector< unique_ptr<Base> >
V
;
/* The dirty trick I spoke about. */
template<typename T> void
init
( T && t )
{
V.emplace_back( new T( forward<T>(t) ) );
return;
};
template<typename T , typename ... U> void
init
( T && t , U && ... u )
{
V.emplace_back( new T( forward<T>(t) ) );
/* The usage of std::move() is explained below. */
init(move(u)...);
return;
};
public:
template<typename ... T>
Worker
( T && ... t )
{
/* Use std::move() because, inside the body
* of the function, the arguments are lvalues.
* If I hadn't put std::move(), the compiler
* would complain about an attempt of using
* _new_ with a reference type (above). */
init(move(t)...);
return;
};
void
work
( void )
{
for ( const auto & x : V )
x->print();
return;
};
};
int
main
( void )
{
/* The goal: be able to create an instance of Worker
* passing temporaries to the constructor. No initializer_list
* is involved, no copies are made; clean, fast moves. */
Worker worker{ Derived1() , Base() , Derived2() };
/* This should print "Derived1\nBase\nDerived2\n". */
worker.work();
return 0;
}
尽管它编译得很好(g++ 4.8.1)并且开箱即用,但我觉得这不是实现我的目标的最佳方式,我想摆脱那种烦人的感觉。有没有人为此找到另一种解决方法?有没有更好的方法,我的设计有什么缺点?
提前致谢。代码应该编译得很好,并展示了我希望如何设计我的界面以及为什么我使用了这些“技巧”。最好的问候, Kalrish