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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:23:32+00:00 2026-05-27T17:23:32+00:00

I’m trying to a design for a system where the user can define their

  • 0

I’m trying to a design for a system where the user can define their own class as an aggregate of a number of predefined components, and then have this class work with algorithms which I provide. I am trying to do this with compile time and/or template based approaches rather than run time polymorphism or virtual functions as performance is important in this case.

For example, consider that I have a number of components which can be used to build a 3D vertex. I will define these components as Position, Normal, Color, etc, and then the user will be able (via multiple inheritance, composition, or what?) to define a vertex such as PositionAndColorVertex which has only position and color but no normal . Now, I provide a function which does some processing on a vector of one million these vertices:

template<typename UsersVertexType>
void myProvidedAlgorithm(std::vector<UsersVertexType> input)
{
    if(vertex has a position)
        //do stuff with position
    if(vertex has a normal)
        //do stuff with normal
    if(vertex has a color)
        //do stuff with color
}

Now, I don’t know what UsersVertexType will look like but it will be built from my components. My functions needs to do something with each of the components but only if they exist. What is an elegant and fast (compile time) way of expressing this?

Of course, I could define a base class for each type, make the user inherit from the desired base classes, and then use dynamic_cast to check which components are implemented, but this is exactly the sort of runtime approach I would like to avoid. Perhaps I can check this inheritance relationship at compile time (the compiler should know what UsersVertexType actually is, right?).

Perhaps my components should be expressed using C++ concepts or policies? I’ve also seen talk of mixins but not sure these are useful. Should the users class use multiple inheritance or composition? Maybe I should somehow get a set of flags into the users class, indicating what it contains? How would you design this system?

Thanks for any insight!

Note: There are similarities to my previous question, but here I am taking a step back and looking for higher level design options/alternatives.

  • 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-27T17:23:32+00:00Added an answer on May 27, 2026 at 5:23 pm

    Traits and template specializations.

    #include <iostream>
    template <typename V> struct traits; // Primary template.
    

    Then define a version for your vertices with positional component, and one for vertices without:

    template <typename Vertex, bool has_position=traits<Vertex>::has_position> 
    struct some_position_op;
    
    template <typename Vertex> struct some_position_op<Vertex,false> {
        void operator() () { std::cout << "has no position.\n"; }
    };
    
    template <typename Vertex> struct some_position_op<Vertex,true> {
        void operator() () { std::cout << "has position.\n"; }
    };
    

    Finally, for each vertex-type you define, implement a traits class:

    struct MyVertexWithPosition {};
    template <> 
    struct traits<MyVertexWithPosition> {
        static constexpr bool has_position = true;
    };
    
    
    struct MyVertexWithoutPosition {};
    template <> 
    struct traits<MyVertexWithoutPosition> {
        static constexpr bool has_position = false;
    };
    

    … and have fun:

    template <typename Vertex>
    void shade (Vertex const &vtx) {
        some_position_op<Vertex>() ();
    }
    
    int main () {
        shade (MyVertexWithPosition());
        shade (MyVertexWithoutPosition());
    }
    

    You could also specialize function templates, but would have to sacrifice some of the readability of your shade function.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.