Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9223887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:10:55+00:00 2026-06-18T04:10:55+00:00

I have a preprocessing code to choose which vector class is used as following:

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:10:56+00:00Added an answer on June 18, 2026 at 4:10 am

    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.

    #define USE_BOOST_VECTOR
    
    #ifdef USE_BOOST_VECTOR
        #include <boost/container/vector.hpp>
        template <typename T> using VECTOR = boost::container::vector<T>;
    #else
        #include <vector>
        template <typename T> using VECTOR = std::vector<T>;
    #endif
    
    // ....
    // You could then use it with
    VECTOR<int> vec;
    

    Finally, here is some more information on templated typedef aliases

    If you don’t have a C++11 compiler, you could use namespace aliases:

    #define USE_BOOST_VECTOR
    
    #ifdef USE_BOOST_VECTOR
        #include <boost/container/vector.hpp>
        namespace Container = boost::container;
    #else
        #include <vector>
        namespace Container = std;
    #endif
    
    //And use it with...
    Container::vector<int> vec;
    

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function called PreProcessSource, which allocates a boost::wave::context and does some preprocessing;
have written this little class, which generates a UUID every time an object of
I have the following C struct from the source code of a server, and
I have a piece of code that is preprocessing submitted form. Currently it was
I'm using an open source library which seems to have lots of preprocessing directives
Something strange happens. I have in my code in a local function #ifdef abc
I have written a script in haskell named testscript with the following code: #!/usr/bin/env
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Have deployed numerous report parts which reference the same view however one of them
I have the following macro that is intended to generate functions in the current

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.