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

  • Home
  • SEARCH
  • 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 3227808
In Process

The Archive Base Latest Questions

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

I am developing the design of an application and I thought I might apply

  • 0

I am developing the design of an application and I thought I might apply some sort of the Visitor design pattern, but it turned out that it’s not exactly what I am looking for. Maybe someone can point me to the variant I require in this case?

Much of my code has a template argument “ContainerType” like

template <class ContainerType>
class MyClass
{
public:
  void doSomething(ContainerType& container) { ... }
};

There is a currently small but growing number of “Containers” that usually share many data fields.

template<class ContainedType>
struct ContainerBase 
{ 
  ContainedType data;
};

struct Container1: ContainerBase<A>, ContainerBase<B>
{};
struct Container2: ContainerBase<A>, ContainerBase<C>
{};

Container1 and Container2 are now used as template arguments for MyClass (and others), where A,B,C are some defined classes. (I have some method to do something like get<A>(container) to access the contained data. This design provides compile-time safety that MyClass may be used with all container types that contain the required types.)

Now I would like to add the feature that “if the Container contains a certain type (e.g. A), then do something, otherwise do nothing”.

This can be done with something that looks like the visitor (but note that no virtual methods are used). It even allows “if the Container contains A do this, if it contains D do something else, otherwise do nothing”. This could be done with

template <class ContainerType>
class MyClass
{
public:
    void doSomething(ContainerType& container) 
    { 
        container.accept(*this); 
    }

    void visit(B& b){...}
    void visit(D& d){...}

    template<typename T>
    void visit(T& t){}
};


struct Container1: ContainerBase<A>, ContainerBase<B>
{
    template<class T>
    void accept(T& t)
    {
        t.visit(ContainerBase<A>::data);
        t.visit(ContainerBase<B>::data);
    }
};

This is what I wanted, but I am looking for a better way to do it, because the implementation shown here requires implementing accept for every ContainerType. If someone derives from Container1 and ContainerBase<D> but forgets to expand the accept methods, things will become bad. Even worse, I will require a const and non-const version of accept and some containers contain >5 types, so that won’t look pretty either.

All container classes are build up by inheriting from ContainerBase<T> multiple times, so I wondered if I could use this structure to implement the accept (and accept(..) const) in the ContainerBase class? I already looked at Lokis typelists but I don’t know how to use them here. Do you have any idea?

Or is it possible to do this thing without the visitor-like structure?

Thanks a lot!

EDIT: I know I could go with RTTI but I’d like to avoid runtime checks and virtual methods if possible.

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

    If you can change the way container classes are defined, it looks like it could be very easy to achieve with Boost.Fusion

    For Example

    #include <iostream>
    
    #include <boost/fusion/container/vector.hpp>
    #include <boost/fusion/algorithm/iteration/for_each.hpp>
    
    struct A {};
    struct B {};
    struct C {};
    
    namespace fusion = boost::fusion;
    
    struct Container1 : fusion::vector< A, B > {};
    
    struct Container2 : fusion::vector< A, C > {};
    
    struct doSomethingWithData {
        void operator()( B &b ) const
        {
            std::cout << "do something with B" << std::endl;
        }
    
        void operator()( C &c ) const
        {
            std::cout << "do something with C" << std::endl;
        }
    
        template < typename T >
        void operator()( T &t ) const
        {
            std::cout << "Neither B nor C" << std::endl;
        }
    };
    
    template < typename ContainerType >
    void doSomething( ContainerType &container )
    {
        fusion::for_each( container, doSomethingWithData() );
    }
    
    int main()
    {
        Container1 c1;
        doSomething( c1 );
        std::cout << "----------------------" << std::endl;
        Container2 c2;
        doSomething( c2 );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a webapp and I'm not sure which pattern design should I use.
Background Story: I am developing a GWT application, using the standard MVP design pattern,
I am developing an application that wants me to design a functionality of posting
I am developing an application that requires some customization by the enduser afecting the
I am developing an application that has a small feature of sending some SMS
Im developing an application where the home view will use dashboard design. I saw
I am developing an application and part of the design is there is a
I am developing a javascript application in which it is possible to design your
We are developing an ASP.NET application. We retained an outside UI design firm, and
Well, I´m developing a wpf application and I got a strange error on design

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.