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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:44:38+00:00 2026-05-16T01:44:38+00:00

There is a simple POD data type like struct Item { int value1; double

  • 0

There is a simple POD data type like

struct Item {
   int value1;
   double value2;
   bool value3;
}

Now I would like to write different count functions like it could be done with the following code (or some std method):

typedef bool Selector(const Item& i);
int count(const vector<Item>& items, Selector f) {
  int sum = 0;
  BOOST_FOREACH(const Item& i, items) {
    if(f(i)) {
      sum++;
    }
  }
  return sum;
}

with f e.g.

bool someSimpleSelector(const Item& i) {
  return i.value1 > 0; // quite simple criterion
}

However in this approach the compiler can not inline the function call and thus will not inline my (trivial) selection code.

My question would be: is there a possibility to implement the above code in a way, where the compiler can inline my selection code, but without implementing the whole count function again and again explicitely (for example by using templates)?

  • 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-16T01:44:38+00:00Added an answer on May 16, 2026 at 1:44 am

    Replace the typedef with a template parameter, to allow generic functors:

    template <typename Selector>
    int count(const vector<Item>& items, const Selector &f)
    

    Then replace your functions with function objects:

    struct someSimpleSelector
    {
        bool operator()(const Item& i) const { return i.value1 > 0; }
    };
    

    You should find that this gets inlined, with suitable compiler optimisation settings.

    By the way, this particular problem can be solved using std::count_if().

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

Sidebar

Related Questions

Is there a simple way to insert the current time (like TIME: [2012-07-02 Mon
Is there a simple way to move an element inside its own parent? Like
Are there simple ways to print debug strings in Rails? Something like the OutputDebugString()
Is there simple solution to build tabs with the scrolling tabs-pane in jQuery? Like
Here by simple, I mean a class with non-virtual empty destructor or POD type.
Is there some simple way in PyQt to store data from QTableWidget to some
I need to convert Pod to HTML. There are number of Pod::HTML and Pod::Simple::*
Let's we have a simple structure (POD). struct xyz { float x, y, z;
Is there a simple way in NumPy to flatten type object array? I know
There are simple 2d array with some sort of tree like this: node1 node2

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.