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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:01:38+00:00 2026-06-16T17:01:38+00:00

I’m trying to use boost tuples to avoid some virtual function overhead, and I

  • 0

I’m trying to use boost tuples to avoid some virtual function overhead, and I can’t quite make it work. I have a vector of “handlers” that try to handle an input, and once one of them returns true, I don’t want to call the rest of them.

C++11 is not available.

First, the current virtual implementation looks like this:

std::vector<Handler*> handlers;

//initialization code...
handlers.push_back(new Handler1);
handlers.push_back(new Handler2);
handlers.push_back(new Handler3);

//runtime code
for(std::vector<Handler*>::iterator iter = handlers.begin();
    iter != handlers.end() && !(*iter)->handle(x); ++iter) {}

Since I have all the types at compiletime, I’d prefer to be able to express this as a tuple, like this:

boost::tuple<Handler1,Handler2,Handler3> handlers;

//runtime code
???
// should compile to something equivalent to:
// if(!handlers.get<0>().handle(x))
//   if(!handlers.get<1>().handle(x))
//     handlers.get<2>().handle(x);

Ideally there would be no virtual function calls and any empty function bodies would inline out. It seems like this might almost be possible with boost::fusion for_each, but I need the short circuiting behavior where once one of the handlers returns true, the rest of them are not called.

  • 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-06-16T17:01:39+00:00Added an answer on June 16, 2026 at 5:01 pm

    You could try something like this:

    void my_eval() {}
    template<typename T, typename... Ts>
    void my_eval(T& t, Ts&... ts) {
      if(!t.handle(/* x */))
        my_eval(ts...); 
    }
    
    template<int... Indices>
    struct indices {
      using next_increment = indices<Indices..., sizeof...(Indices)>;
    };
    
    template<int N>
    struct build_indices {
      using type = typename build_indices<N - 1>::type::next_increment;
    };
    template<>
    struct build_indices<0> {
      using type = indices<>;
    };
    
    template<typename Tuple, int... Indices>
    void my_call(Tuple& tuple, indices<Indices...>) {
        my_eval(std::get<Indices>(tuple)...);
    }
    template<typename Tuple>
    void my_call(Tuple& tuple) {
        my_call(tuple, typename build_indices<std::tuple_size<Tuple>::value>::type());
    }
    

    To use just pass your tuple to my_call.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
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
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

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.