Andrey Tarasevich
11/25/2008 9:42:00 PM
mzdude wrote:
>
> Right now what I have is
>
> #define FooVector(T) std::vector<Foo<T> >
>
> template<typename T>
> FooVector(T) BuildList()
> {
> FooVector(T) v;
> v.push_back(T(5,0.1));
> return v;
> }
>
> but I want to get rid of the macro.
>
> Right now I'm struggling with the syntax of declaring a vector of
> Foo objects.
>
> What I want to do is
> // the following gives a compile error
> typedef template<typename T> std::vector<Foo<T> > FooVector<T>;
>
> and ultimately
>
> template<typename T>
> FooVector<T> BuildList();
>
> Is it possible? What is the correct syntax?
>
What you need is a "parametrized typedef" (a "template typedef").
Currently there's no such feature in C++, but in many cases you can
simulate it with a macro (which is exactly what you did already), or
better by using a wrapper template
template <typename T> struct FooVector {
typedef std::vector<Foo<T> > type;
};
Usage
template<typename T> FooVector<T>::type BuildList();
That's is a bit inelegant, but that's all we have at this time.
--
Best regards,
Andrey Tarasevich