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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:09:30+00:00 2026-05-19T10:09:30+00:00

I haven’t used the advanced features of C++ for a while and am refreshing

  • 0

I haven’t used the advanced features of C++ for a while and am refreshing my C++ knowledge..
Having said that, the concept of traits and policy based programming was something that I never really managed to get my head around.

I want to change that. I am writing a generic container. I want to enforce a policy that the container will store only classes that derive from a particular base class. This is because the container returns an invalid object (instead of throwing) when an attempt is made to access an item outside the vector bounds.

template <class T>   
class GenericContainer
{
private:
    typedef std::vector<T> TypeVect;
    void addElement(const T& elem);

    TypeVect m_elems;

public:
    unsigned int size() const;
    T& elementAt(const unsigned int pos);
    const T elementAt(const unsigned int pos) const;
};

How would I use traits to restrict this generic container to contain only subclasses of class ‘ContainerItem’ say?

  • 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-19T10:09:31+00:00Added an answer on May 19, 2026 at 10:09 am

    You can use a little IsDerivedFrom template which can only be instantiated in case a given type ‘D’ inherits another type ‘B’ (this implementation was taken from a nice Guru Of The Week article):

    template<typename D, typename B>
    class IsDerivedFrom
    {
      static void Constraints(D* p)
      {
        B* pb = p; // this line only works if 'D' inherits 'B'
        pb = p; // suppress warnings about unused variables
      } 
    
    protected:
      IsDerivedFrom() { void(*p)(D*) = Constraints; }
    }; 
    
    // Force it to fail in the case where B is void
    template<typename D>
    class IsDerivedFrom<D, void>
    {
      IsDerivedFrom() { char* p = (int*)0; /* error */ }
    };
    

    You can now simply instantiate the IsDerivedFrom template using inheritance:

    template <class T>   
    class GenericContainer : public IsDerivedFrom<T, ContainerItem>
    {
        ...
    };
    

    This code only compiles if T inherits ContainerItem.

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

Sidebar

Related Questions

Haven't used C++ in a while. I've been depending on my Java compiler to
Haven't seen this feature anywhere else. I know that the 32nd bit is used
I haven't used the STL much before, but I started to on this huffman
I haven't used ARC yet other than to deal with it when it forces
I haven't used multithreading in Clojure at all so am unsure where to start.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Haven't been programming in JS for a while. Now, I have following thing: <html>
Haven't seen anything, but I have seen that you can create albums in the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
haven't used regex replaces much and am not sure if how I have done

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.