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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:17:54+00:00 2026-05-30T15:17:54+00:00

I wish to modify an existing templated class. The class’s template arguments are (semi)

  • 0

I wish to modify an existing templated class. The class’s template arguments are (semi) variable and I would like to use them to generate a conditional/switch/map like function body.

My compiler does not support variadic templates so the (boost) preprocessor is currently used to generate the existing class:

template <typename item0, typename item1, typename item2 ..., typename itemN>
struct myclass { /*various operations*/ };

A new function func is required that will query variables at run-time and return an object of a that is one of the template arguments.

Example:

template <typename item0, typename item1, typename item2 ...>
struct my_class { 

    //...various operations

    //automatic generatation possible?
    std::string * func()
    {
        string s;
        while(data) {

        switch (data[0])
        {
            case item0::id:
                s += item0::get_name();
            case item1::id:
                s += item1::get_name();
            //... for each template arguemnt typename where typename is no void
        }
        }
        return s;
    }
 };

typedef my_class<a, b, c> class_one;
typedef my_class<d, e, f> class_two;
typedef my_class<a, b, c, x, y, z>  class_three;

 int main()
 {
    ...
    class_one test;
    test.func();
    ...
 }

I would like to generate the contents of func() because the number of items will be numerous, and the number of types of “myclass” will be even higher.

Can someone give me an idea of how any techniques that could accomplished this?

I already have a dependency on boost. My compiler is reasonably new (but does not support variadic templates). I would prefer not to take any new dependencies or introduce more complexity than necesssary.

  • 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-30T15:17:55+00:00Added an answer on May 30, 2026 at 3:17 pm

    I’ve written code like this before, so I can tell you it is possible. (It was for commercial, closed-source work, so I’m afraid I can’t show you the code). You can find a really good example of how to do this in the Boost.Variant library, in particular http://svn.boost.org/svn/boost/trunk/boost/variant/detail/visitation_impl.hpp . The code is very dense and advanced C++, so it might take a day or two to understand it thoroughly.

    A quick summary: the boost::variant class template works like a union with an int storing which member of the union is valid. The “visitation” feature lets you supply a function object with an overloaded operator() that can take any of the possible members of the union, and it generates a switch statement that accesses the appropriate member and calls the right operator() overload on it. If you are already finding this complicated to follow, or you don’t know about Boost.MPL yet, I suggest you stop reading here, read the Boost.Variant documentation, and then rewrite your class to be able to use that: the clever guys at Boost have already done the work for you. It’s header-only, so if you’re already using Boost there’s no new dependency for you.

    This file is in charge of generating the switch statement. In short, it has two alternative implementations. The first one (lines 72-90) uses a recursive template visitation_impl_step that works like the factorial function you might have seen as a template meta-programming example. The unspecialized template recursively calls the next one in the list (typename mpl::next<Iter>::type). The resulting code, once all the templates are expanded out, looks a bit like a series of functions function0, function1, &c. like this:

    result_type functionN(variant vnt, visitor vr) {
        if (v.which == N)
            return vr(static_cast<Nth type>(vnt.value));
        else
            functionN-1(vnt, vr);
    }
    

    The second implementation (lines 193-285) uses the Boost.PP preprocessor magic library to generate a switch statement just like the one you want, with as many cases as a boost::variant can possibly have. The body of each case is a call to a template function (lines 120-185) which generates a call to the visitor on the Nth type. Most of the complexity in this implementation comes from having to worry about backing up the value inside the variant in order to preserve the strong exception guarantee if the visitor or any involved constructor throws.

    Even if you decide to do it another way, I recommend reading and understanding the Boost.Variant source code, as a learning exercise. It will redefine your ideas of what is possible (and what is sensible) in C++!

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

Sidebar

Related Questions

I wish to implement a 2d bit map class in Python. The class would
I wish to use xml and xsl to generate controls on an asp.net page.
I wish to modify data by selecting them in a inner query and count
I wish to directly modify a variable's value outside of a method from inside
I wish to test a function that will generate lorem ipsum text, but it
I have the following html that I am trying to modify: <div class=col1 width8><img
I have an existing JComboBox object. I can modify many of its properties using
I wish to modify Rprofile.site file under Windows 7 R-2.11.1 using Notepad++ editor changing
I'm using Hi-Tech C compiler and wish to use Scons to build my projects.
I have an array a whose values I wish to modify through another function.

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.