typedef a vector of fixed size

Started by
3 comments, last by alnite 17 years, 11 months ago
I can typedef an array like this: typedef double stuff[100]; But how do I typedef a vector of fixed size? typedef std::vector<double> stuff(100); doesn't seem to work. This is not really important because I can always put 100 in the constructor, but I think it would be convenient to have it typedefed.
Advertisement
Nope, this is not possible. There is a good reason for this: it would mean that the type of stuff could be changed at run time, which isn't how C++ works.
i 'd suggest using boost::array for stuff like that, if you're keen on using boost, that is.
That is not possible and is nonsensical, what does it mean to give a type alias for an invocation of a constructor?. Typedefing a statically sized array works because A.) it is a type or rather it has a type B.) the size is known at compile-time which isn't generally the case for std::vector (i say generally because of custom allocator types that can be given to standard library containers) and even it was it still wouldn't work because you're trying to do something in run-time land in compile-time land.
Quote:Original post by snk_kid
That is not possible and is nonsensical, what does it mean to give a type alias for an invocation of a constructor?. Typedefing a statically sized array works because A.) it is a type or rather it has a type B.) the size is known at compile-time which isn't generally the case for std::vector (i say generally because of custom allocator types that can be given to standard library containers) and even it was it still wouldn't work because you're trying to do something in run-time land in compile-time land.

Yes, I am aware of that, and that's why I am asking it here because maybe there are members who have done something similar and have found an alternative solution/trick (e.g. boost::array as AP suggested). The design of the class that will use this array requires a lot of copying/deleting/processing of this array, and they are all fixed in size. Obviously using the standard array would cost me more coding for bookeeping the array, so I use std::vector. The current approach, which is to pass the size in every instantiation of this array, works, but I'd just like to know if it's possible to typedef the size too because that would then make my life a lot easier and a lot safer.

boost::array is attractive, and is just what I need. Thanks for the suggestion.

This topic is closed to new replies.

Advertisement