I have a preprocessing code to choose which vector class is used as following:
#define USE_BOOST_VECTOR
#ifdef USE_BOOST_VECTOR
#include <boost/container/vector.hpp>
#define VECTOR boost::container::vector
#else
#include <vector>
#define VECTOR std::vector
#endif
I am not sure if this is a good way. And how about if I have more options to choose, what should I do?
Figured out if there are more options
#define USE_MY_VECTOR 1
#define USE_BOOST_VECTOR 2
#define USE_STD_VECTOR 3
#define CHOOSE_VECTOR USE_BOOST_VECTOR
#if CHOOSE_VECTOR == USE_MY_VECTOR
#include "Vector.h"
#define VECTOR Vector
#elif CHOOSE_VECTOR == USE_BOOST_VECTOR
#include <boost/container/vector.hpp>
#define VECTOR boost::container::vector
#elif CHOOSE_VECTOR == USE_STD_VECTOR
#include <vector>
#define VECTOR std::vector
#endif
But I need to define 1, 2, 3, and more for more options. Just for a brain work, any better way?
Don’t use pre-processor macros unless you have no other choice – typedefs, constants and if really needed, templates usually can have the intended result, while being much safer.
Now, unless you have a really good reason to prefer Boost vectors over std::vector, go with std::vector. Why? Because if you don’t know what you want, what the std namespace provides should be more than enough.
EDIT: Like leemes says, you would need C++11 to make use of templated type aliases.
Finally, here is some more information on templated typedef aliases
If you don’t have a C++11 compiler, you could use namespace aliases:
I also need to add here, that the above method can lead to some very confusing results. I highly recommend that you basically decide up front which vector implementation you’d like to go with and getting rid of your pre-processor macro once and for all. Your code will thank you over time 🙂