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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:37:23+00:00 2026-05-28T16:37:23+00:00

I have my own array class template I would like to optionally add functionality

  • 0

I have my own array class template I would like to optionally add functionality to.

As an example of functionality, take multithreading support: in some cases, I need arrays that put #pragma omp atomic just before any update code (a compiler directive that enforces atomic behaviour, the details aren’t important). In other cases, I need arrays that don’t do this, as I know they will only be updated safely and I need to avoid the performance hit.

Intuitively it should be possible to define a class called AtomicUpdates that I can inherit from. So to define a double array with atomic updates I would say something like

class AtomicDoubleArray : public MyArray<double>, public AtomicUpdates {};

But I can’t see how you’d implement that in practice, and also this would break the principle of inherit interface, not implementation.

Can anyone enlighten me as to what I really want to do here?

  • 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-05-28T16:37:24+00:00Added an answer on May 28, 2026 at 4:37 pm

    Even if you don’t end up using them now mixins and policy template arguments are very useful things to understand. In this case they are very similar. First, an array with a mixin base. I have used c++0x mutex rather than openmp but you should get the idea.

    #include <iostream>
    #include <vector>
    #include <mutex>
    
    template <class value_t, class base_t>
    class array_t : private base_t {
        std::vector<value_t> v_;
    public:
        array_t(size_t sz = 0) : v_ (sz) { }
        value_t get(size_t i) const
        {
            this->before_get();
            value_t const result = v_[i];
            this->after_get();
            return result;
        }
        void set(size_t i, value_t const& x)
        {
            this->before_set();
            v_[i] = x;
            this->after_set();
        }
    };
    
    class no_op_base_t {
    protected:
        void before_get() const { }
        void after_get() const { }
        void before_set() const { }
        void after_set() const { }
    };
    
    class lock_base_t {
        mutable std::mutex m_;
    protected:
        void before_get() const { std::cout << "lock\n"; m_.lock(); }
        void after_get() const { std::cout << "unlock\n"; m_.unlock(); }
        void before_set() const { std::cout << "lock\n"; m_.lock(); }
        void after_set() const { std::cout << "unlock\n"; m_.unlock(); }
    
    };
    
    int main()
    {
        array_t<double, no_op_base_t> a (1);
        array_t<double, lock_base_t> b (1);
        std::cout << "setting a\n";
        a.set(0, 1.0);
        std::cout << "setting b\n";
        b.set(0, 1.0);
        std::cout << "getting a\n";
        a.get(0);
        std::cout << "getting b\n";
        b.get(0);
        return 0;
    }
    

    Now the same class but using a policy argument approach rather than inheritance.

    #include <iostream>
    #include <vector>
    #include <mutex>
    
    template <class value_t, class policy_t>
    class array_t {
        policy_t policy_;
        std::vector<value_t> v_;
    public:
        array_t(size_t sz = 0) : v_ (sz) { }
        value_t get(size_t i) const
        {
            policy_.before_get();
            value_t const result = v_[i];
            policy_.after_get();
            return result;
        }
        void set(size_t i, value_t const& x)
        {
            policy_.before_set();
            v_[i] = x;
            policy_.after_set();
        }
    };
    
    class no_op_base_t {
    public:
        void before_get() const { }
        void after_get() const { }
        void before_set() const { }
        void after_set() const { }
    };
    
    class lock_base_t {
        mutable std::mutex m_;
    public:
        void before_get() const { std::cout << "lock\n"; m_.lock(); }
        void after_get() const { std::cout << "unlock\n"; m_.unlock(); }
        void before_set() const { std::cout << "lock\n"; m_.lock(); }
        void after_set() const { std::cout << "unlock\n"; m_.unlock(); }
    
    };
    
    int main()
    {
        array_t<double, no_op_base_t> a (1);
        array_t<double, lock_base_t> b (1);
        std::cout << "setting a\n";
        a.set(0, 1.0);
        std::cout << "setting b\n";
        b.set(0, 1.0);
        std::cout << "getting a\n";
        a.get(0);
        std::cout << "getting b\n";
        b.get(0);
        return 0;
    }
    

    In this case both are very similar. The important difference is that the mixin could define some methods to be virtual and allow you to change the behaviour of array by inheriting from it. As in the following:

    template <class value_t>
    class mk_virtual_base_t {
    protected:
        void before_get() const { }
        void after_get() const { }
        void before_set() const { }
        void after_set() const { }
    
        virtual value_t get(size_t) const = 0;
        virtual void set(size_t, value_t) = 0;
    };
    
    template <class value_t>
    class daily_wtf_contender_t : public array_t<value_t, mk_virtual_base_t<value_t> > {
        virtual value_t get(size_t) const { std::cout << "surprise! get is virtual!\n"; return 0; }
        virtual void set(size_t, value_t) { std::cout << "surprise! set is virtual!\n"; }
    };
    

    While there are some real cases where the mixin advantage is useful it is not that often. So when working with templates the policy approach is often the more appropriate. Policy arguments are used by the standard library in many places so there are some good examples for you to study.

    As for your question about “inherit interface, not implementation.” Used carefully inheriting implementation is quite useful. Same goes for multiple inheritance. You just need to be judicious about when you use them.

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

Sidebar

Related Questions

How would one return a class computed CSS property/property array? Like, if I have
I'm creating my own JavaScript Array-like object and I have methods that call closures.
I am developing my own class model for my database and I would like
I'd like to create my own class extending array of ints. Is that possible?
For School I have been given a class interface to define my own array
I have a question about templates. I would like to have a templated class
I'm trying to hide window after its startup. I have own window-class which is
I have my own asp.net cookie created like this: var authTicket = new FormsAuthenticationTicket(
How would you handle grouping different objects? For example, lets say you have a
I'd need a class like std::auto_ptr for an array of unsigned char*, allocated with

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.