1
我学习的boost :: MPL和我有下面的类 -如何实例化一个没有默认构造函数的类型的boost :: fusion :: vector成员变量?
#include <string>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/at.hpp>
#include <boost/fusion/include/mpl.hpp>
#include <boost/fusion/container.hpp>
using namespace boost;
using namespace std;
template< typename T >
class Demo
{
public:
typedef boost::mpl::size<T> NumDimensions;
template< size_t D >
struct Dim
{
typedef typename boost::mpl::at_c< T, D >::type Type;
};
template< size_t D >
typename Dim<D>::Type& GetElement()
{
return fusion::at_c<D>(elements_);
}
private:
typename fusion::result_of::as_vector<T>::type elements_;
};
这工作得很好,只要我使用类型的默认构造函数(或默认类型)
int main(int argc, char *argv[])
{
typedef Demo< boost::mpl::vector< int, std::string > > D1;
D1 d;
D1::Dim<0>::Type &i = d.GetElement<0>();
i = "Hello World!";
cout << " " << i << endl;
}
然而,如果我使用没有默认构造函数的类型,它会引发编译器错误,因为矢量初始化失败。有没有一种标准的方式来正确地初始化成员(在构造函数中)而不诉诸指针/引用?