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 7862847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:17:55+00:00 2026-06-02T23:17:55+00:00

The project that I’m working on could benefit from having the possibility to easily

  • 0

The project that I’m working on could benefit from having the possibility to easily swap between different big number libraries: GMP, OpenSSL, etc. My current implementation defines a template abstract base class in which I implement all the required operators (just to have some syntactic sugar) and I define the required pure virtual functions.

For each library, I have a derived class like this: class BigNumberGmp : public BigNumberBase<BigNumberGmp>. I know it kinda’ breaks OOP, but the C++ covariance functionality is too restrictive and it does not allow me to return BigNumberBase objects from methods, only references, which is quite undesirable…

The problem is that I want the code that uses my custom wrappers to be able to work with any such wrapper: BigNumberGmp, BigNumberOpenSsl, etc. In order to achieve this, I defined typedef BigNumberGmp BigNumber and put it inside some conditional macros, like so:

#if defined(_LIB_GMP)
    typedef BigNumberGmp BigNumber;
#endif

Also, I include the appropriate headers in a similar fashion. This implementation requires that I define the _LIB_GMP symbol in the compiler options.

As you can see, this is a rather hackish technique, that I’m not really proud of. Also, it does not hide in any way the specialized classes (BigNumberGmp, BigNumberOpenSsl, etc). I could also define the BigNumber class multiple times, wrapped in _LIB_XXX conditional macros or I could implement the required methods inside the BigNumber class multiple times, for each library, also wrapped in the _LIB_XXX conditional macros. These two latter ideas seem even worse than the typedef implementation and they will surely mess up the doxygen output, since it will not be able to figure out why I have multiple items with the same name. I want to avoid using the doxygen preprocessor, since I still depend on the _LIB_XXX defines…

Is there an elegant design pattern that I could use instead? How would you approach such a problem?

  • 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-02T23:17:56+00:00Added an answer on June 2, 2026 at 11:17 pm

    It looks like you’re going to recompile each time you switch libraries in which case you can use template specialisation instead of inheritance.

    Choosing which to use would be pretty much as you have it (something #if based), but you’d save the virtual members which means the compiler can still inline which means it could be significantly faster for some cases.

    First of all, take structs that describe each of the implementations. In here you can put the basic API names that work in the same sort of way across all of the libraries. For example, if they all support an add operation that takes pointers to two big nums and returns a pointer to a new big num that contains the result you can do something like this:

    (Note that I’ve not run this through a compiler, and I don’t know what the actual APIs look like, but it should be enough to give a general idea of the approach)

    struct GMP {
        GMP_ptr* add(GMP_ptr *l, GMP_ptr*r) {
            return GMPadd(l, r);
        }
    };
    struct OpenSSL {
        OpenSSL_ptr* add(OpenSSL_ptr*, OpenSSL_ptr*) {
            OpenSSL_ptr ret = NULL;
            OpenSSLadd(l, r, &ret);
            return ret;
        }
    };
    

    Now we can define a common super class that contains the use of these easy to map APIs:

    template< typename B, typename R >
    class common {
    public:
        // Assume that all types have the same API
        R operator + (const common &r) {
            return R(B::add(l.ptr, r.ptr));
        }
    };
    

    The type B is the struct that defines the big number API, and the type R is the real implementation sub-class. By passing in R like this we solve the co-variant return problem.

    For the real implementation we define a template that will do the work for us:

    template< typename B >
    class big_num;
    

    Now we can specialise this for the implementations:

    template<>
    class big_num<OpenSSL> : common< OpenSSL, big_num<OpenSSL> > {
        OpenSSL_ptr *ptr;
    public:
        big_num(OpenSSL_ptr*p)
        : ptr(p) {
        }
        big_num(const char *s)
        : ptr(OpenSSLBigNumFromString(s)) {
        }
        ~big_num() {
            OpenSSLBigNumFree(ptr)
        }
    };
    

    The operator + will come from the super class, and you can now use them like this:

    void foo() {
        big_num< GMP > gmp1("123233423"), gmp2("234");
        big_num< GMP > gmp3 = gmp1 + gmp2;
        big_num< OpenSSL > ossl1("1233434123"), ossl2("234");
        big_num< OpenSSL > ossl3 = ossl1 + ossl2;
    }
    

    The advantage here is that there is minimum code duplication between specialisations due to the use of the structs to adapt between similar API features and a common implementation in one template. The specifics for a given API are now in the template specialisations, but there are no virtuals and there is no common super class. This means that the compiler can inline pretty much everything in your wrappers which will make them essentially as fast as they can be.

    Due to the specialisation you also potentially have access to all of the implementations which may make your unit tests much easier to write/manage (you should be able to write templated versions of those too).

    If you only want one of them to be visible then something like this:

    #if BIGNUM=="GMP"
        typedef big_num<GMP> used_big_num;
    #elif BIGNUM=="OpenSSL"
         typedef big_num<OpenSSL> used_big_num;
    #endif
    

    You may also need to put guards around the specialisations too if the headers aren’t always available in which case you’ll want a set of HAVE_GMP_BIGNUM and HAVE_OPENSSL_BIGNUM macros too.

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

Sidebar

Related Questions

I have a project that is supposed to run on different (at least 2)
I'm working on project that lets users choose some scientific authors and columnists and
The project that I am currently working on at the moment uses SVN for
I am doing a project that has dependencies on some classes from the mahout
I have a big project that also use many libraries. With jstack I found
The project that I've been working on involves porting some old code. Right now
The project that I'm working on at the moment uses an IDisposable object in
The project that I'm working on uses a commercially available package to route audio
The project that I'm currently working on, is large scale. I'm using email activation
The project that I'm working on needs to upload videos to S3. It uses

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.