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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:52:35+00:00 2026-06-14T08:52:35+00:00

Have a look at this hypothetical header file: template <class T> class HungryHippo {

  • 0

Have a look at this hypothetical header file:

template <class T>
class HungryHippo {
public:
    void ingest(const T& object);
private:
    ...
}

Now, for a HungryHippo<string> it makes sense that you would want to ingest references to the strings — copying a string might be very expensive! But for a HungryHippo<int> it makes way less sense. Passing an int directly can be really cheap (most compilers will do it in a register), but passing a reference to an int is an extra needless level of indirection. This all applies to returning values as well.

Is there some way to suggest to the compiler “hey, I’m not going to modify the argument, so you decide whether to pass by value or by reference, depending on what you think is better”?

Some things that may be relevant:

  • I can fake this effect manually by writing template <class T, bool PassByValue> class HungryHippo and then specializing on PassByValue. If I wanted to get really fancy, I could even infer PassByValue based on sizeof(T) and std::is_trivially_copyable<T>. Either way, this is a lot of extra work when the implementations are going to look pretty much the same, and I suspect the compiler can do a much better job of deciding whether to pass by value than I can.
  • The libc++ project seems to solve this by inlining a lot of functions so the compiler can make the choice one level up, but in this case let’s say the implementation of ingest is fairly complicated and not worth inlining. As explained in the comments, all template functions are inline by default.
  • 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-14T08:52:37+00:00Added an answer on June 14, 2026 at 8:52 am

    The boost::call_traits header deals with exactly this issue. Check it out here.

    Specifically, the call_traits<T>::param_type option includes the following description:

    If T is a small built in type or a pointer, then param_type is defined
    as T const, instead of T const&. This can improve the ability of the
    compiler to optimize loops in the body of the function if they depend
    upon the passed parameter, the semantics of the passed parameter is
    otherwise unchanged (requires partial specialization).

    In your case, you could define ingest as follows:

    template <class T>
    class HungryHippo {
    public:
        void ingest(call_traits<T>::param_type object);
        // "object" will be passed-by-value for small 
        // built-in types, but passed as a const reference 
        // otherwise
    private:
        ...
    };
    

    Whether this would actually make much of a difference in your actual code/compiler combination, I’m not sure. As always, you’d have to run some actual benchmarks and see what happens…

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

Sidebar

Related Questions

Have a look at this page: http://173.203.72.190:12123/Search-Results.aspx?Mode=Browse&Cuisine=-1&Zone=-1 If you click 'Browse' with the 'Any
Have a look at this HTML code: <div class=overlay> <a href=#>1</a> <a href=#>1</a> <a
Have a look at this code: ArrayList<Object> row = data.get(position); TextView idText = new
Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
Have a look to this code snippet:- -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [webData setLength:
Have a look at this Scala class: class Example { val (x, y): (Int,
Have a look at this pseudocode: void* magicpointer = malloc(OVER_NINE_THOUSAND); NSValue* v = [NSValue
Have a look at this picture alt text http://www.abbeylegal.com/downloads/2009-04-01/web%20part%20top%20line.jpg Does anyone know what css
Have a look a this simple example. I don't quite understand why o1 prints
Have a look at this jsfiddle: http://jsfiddle.net/Dh96F/1/ The Expand button will animate the widening

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.