You can copy the solution from Boost.Assign
.
Something like the code below - however, we can consider it "elegant" only if done once, used many times.
Purpose:
MYTYPE mm[] = {
{ VectorBuilder<int>(1)(2)(3), 4},
{ VectorBuilder<int>(3)(4)(5), 4}
};
Solution ( working example ):
template <typename T>
class VectorBuilder
{
public:
VectorBuilder(T val) : val(val), prev(0), next(0), first(this)
{}
VectorBuilder operator ()(T val) const
{
return VectorBuilder(val, this);
}
operator std::vector<T>() const
{
std::vector<T> vv;
vv.reserve(this->getSize());
this->build(vv);
return vv;
}
private:
T val;
const VectorBuilder* prev;
mutable const VectorBuilder* next;
const VectorBuilder* first;
VectorBuilder(T val, const VectorBuilder* prev)
: val(val), prev(prev), next(0), first(prev->first)
{
prev->next = this;
}
std::size_t getSize() const
{
}
void build(std::vector<T>& vv) const
{
}
};
source
share