Jump to content
  • Advertisement
Sign in to follow this  
alnite

typedef a vector of fixed size

This topic is 4552 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i 'd suggest using boost::array for stuff like that, if you're keen on using boost, that is.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!