?
=?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=
Hello!
Sometimes I get the urge to write something like the following template
(see the code below) to help me initialise stl containers. With this
template I could:
vector<int> v = Initializer<int, vector>()(1)(-1)(0);
instead of:
vector<int> v;
v.push_back(1);
v.push_back(-1);
v.push_back(0);
This brings a nice warm feeling to my tummy. Not only is it short and
clear but it seems I can also use this to initialise a static container
in one statement. I could also use it in an intialiser list of a
constructor if I wanted to:
class A
{
public:
A() : v_(Initializer<int, std::vector>()(1)(2)) { }
private:
std::vector<int> v_;
};
But something in the back of my head tells me that my initialiser
template is not 100% kosher.
Has anybody got some insight into this subject? Why haven't I seen a
template like this anywhere, in litterature or in real life projects? Is
there an achilles heel to my approach that I have missed?
Show me the light!
:.:: brasse
/**
* @brief
* This class helps with initialisation of containers.
*
* The following example shows how to decalare and initialise a
* <code>vector</code> of integers in one statement:
*
* @code
* vector<int> v = Initializer<int, vector>()(1)(-1)(0);
* @endcode
*
* This will give the same result as:
*
* @code
* vector<int> v;
* v.push_back(1);
* v.push_back(-1);
* v.push_back(0);
* @endcode
*
* Note: Using <code>Initializer</code> will actually cause the
* objects to be copied two times.
*/
template <typename T, template <typename T> class C>
class Initializer
{
public:
Initializer() { }
Initializer& operator()(const T& v)
{
c.push_back(v);
return *this;
}
operator C<T>()
{
return c;
}
private:
C<T> c;
};
Sometimes I get the urge to write something like the following template
(see the code below) to help me initialise stl containers. With this
template I could:
vector<int> v = Initializer<int, vector>()(1)(-1)(0);
instead of:
vector<int> v;
v.push_back(1);
v.push_back(-1);
v.push_back(0);
This brings a nice warm feeling to my tummy. Not only is it short and
clear but it seems I can also use this to initialise a static container
in one statement. I could also use it in an intialiser list of a
constructor if I wanted to:
class A
{
public:
A() : v_(Initializer<int, std::vector>()(1)(2)) { }
private:
std::vector<int> v_;
};
But something in the back of my head tells me that my initialiser
template is not 100% kosher.
Has anybody got some insight into this subject? Why haven't I seen a
template like this anywhere, in litterature or in real life projects? Is
there an achilles heel to my approach that I have missed?
Show me the light!
:.:: brasse
/**
* @brief
* This class helps with initialisation of containers.
*
* The following example shows how to decalare and initialise a
* <code>vector</code> of integers in one statement:
*
* @code
* vector<int> v = Initializer<int, vector>()(1)(-1)(0);
* @endcode
*
* This will give the same result as:
*
* @code
* vector<int> v;
* v.push_back(1);
* v.push_back(-1);
* v.push_back(0);
* @endcode
*
* Note: Using <code>Initializer</code> will actually cause the
* objects to be copied two times.
*/
template <typename T, template <typename T> class C>
class Initializer
{
public:
Initializer() { }
Initializer& operator()(const T& v)
{
c.push_back(v);
return *this;
}
operator C<T>()
{
return c;
}
private:
C<T> c;
};