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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:44:32+00:00 2026-05-12T09:44:32+00:00

I have a vector containing large number of elements. Now I want to write

  • 0

I have a vector containing large number of elements. Now I want to write a small function which counts the number of even or odd elements in the vector. Since performance is a major concern I don’t want to put an if statement inside the loop. So I wrote two small functions like:

long long countOdd(const std::vector<int>& v)
{
    long long count = 0;
    const int size = v.size();
    for(int i = 0; i < size; ++i)
    {
        if(v[i] & 1)
        {
            ++count;
        }
    }
    return count;
}

long long countEven(const std::vector<int>& v)
{
    long long count = 0;
    const int size = v.size();
    for(int i = 0; i < size; ++i)
    {
         if(0 == (v[i] & 1))
        {
            ++count;
        }
    }
    return count;
}

My question is can I get the same result by writing a single template function like this:

template <bool countEven>
long long countTemplate(const std::vector<int>& v1)
{
    long long count = 0;
    const int size = v1.size();
    for(int i = 0; i < size; ++i)
    {
        if(countEven)
        {
            if(v1[i] & 1)
            {
                ++count;
            }
        }
        else if(0 == (v1[i] & 1))
        {
            ++count;
        }
    }
    return count;
}

And using it like this:

int main()
{
  if(somecondition)
  {
     countTemplate<true>(vec); //Count even
  }      
  else
  {
     countTemplate<false>(vec); //Count odd
  } 
}

Will the code generated for the template and non-template version be the same ? or will there be some additional instructions emitted?

Note that the counting of numbers is just for illustration hence please don’t suggest other methods for counting.

EDIT:
Ok. I agree that it may not make much sense from performance point of view. But atleast from maintainability point of view I would like to have only one function to maintain instead of two.

  • 1 1 Answer
  • 1 View
  • 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-12T09:44:32+00:00Added an answer on May 12, 2026 at 9:44 am

    Your template version will generate code like this:

    template <>
    long long countTemplate<true>(const std::vector<int>& v1)
    {
        long long count = 0;
        const int size = v1.size();
        for(int i = 0; i < size; ++i)
        {
            if(true)
            {
                    if(v1[i] & 1)
                    {
                            ++count;
                    }
            }
            else if(0 == (v1[i] & 1))
            {
                    ++count;
            }
        }
        return count;
    }
    
    
    template <>
    long long countTemplate<false>(const std::vector<int>& v1)
    {
        long long count = 0;
        const int size = v1.size();
        for(int i = 0; i < size; ++i)
        {
            if(false)
            {
                    if(v1[i] & 1)
                    {
                            ++count;
                    }
            }
            else if(0 == (v1[i] & 1))
            {
                    ++count;
            }
        }
        return count;
    }
    

    So if all optimizations are disabled, the if will in theory still be there. But even a very naive compiler will determine that you’re testing a constant, and simply remove the if.

    So in practice, no, there should be no difference in the generated code. So you can use the template version and don’t worry about this.

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

Sidebar

Related Questions

I have a very large std::vector of std::vectors containing a fixed number of unsigned
I have a vector containing integer values, which I wish to sum. However, it's
I have a vector containing n elements. I need to choose a subset of
If I have a vector containing a number of unsorted files, how can I
Say I have a std::vector called vec with 10 elements and I want to
I have a vector containing a series of integers, and what I want to
Right now I have a vector std::vector<char> myVector(4) containing any combination of a set
I have a map class, which contains a vector containing MapEntitys. MapEntity is a
I have a std::vector containing a handful of numbers, which are not in any
I have a number of vector containers of varying size each containing doubles. I

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.