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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:57:56+00:00 2026-05-26T22:57:56+00:00

Compare generic integration functions: template <class F> double integrate(F integrand); with template <class F>

  • 0

Compare generic integration functions:

template <class F> double integrate(F integrand);

with

template <class F> double integrate(F& integrand);

or

template <class F> double integrate(const F& integrand);

What are the pros and cons of each? STL uses the first approach (pass by value), does it mean it’s the most universal one?

  • 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-26T22:57:56+00:00Added an answer on May 26, 2026 at 10:57 pm

    Function objects usually should be small so I don’t think that passing them by value will suffer from performance noticably (compare it to the work the function does in its body). If you pass by value, you can also gain from code analysis, because a by value parameter is local to the function and the optimizer may tell when and when not a load from a data member of the functor can be omitted.

    If the functor is stateless, passing it as argument implies no cost at all – the padding byte that the functor takes doesn’t have to have any particular value (in the Itanium Abi used by GCC at least). When using references, you always have to pass an address.

    The last one (const T&) has the drawback that in C++03 that doesn’t work for raw functions, because in C++03 the program is ill-formed if you try to apply const to a function type (and is an SFINAE case). More recent implementations instead ignore const when applied on function types.

    The second one (T&) has the obvious drawback that you cannot pass temporary functors.

    Long story short, I would generally pass them by value, unless I see a clear benefit in concrete cases.

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

Sidebar

Related Questions

So I'm attempting to utilize a generic compare functor in my utility class. I
How can I compare two generic collections? Here's my attempt with two string arrays,
Using this example off MSDN: using System.Collections.Generic; using System.IO; namespace CollectionTest { public class
I was thinking about writing generic functions for basic Math operations such as Min,
how can i compare the items in a string[] array against a generic list
When working with the List class from System.Collections.Generic, methods like Contains or IndexOf will
I'm writing a templated C++ generic container class that can optionally maintain its contents
In the tree class I'm suppose to compare two node, for you know searching
How do you go about comparing two generic classes? class Entry<K,V> { protected K
I have a list-generic that has a property (class type). I need a sort

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.