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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:26:55+00:00 2026-05-18T04:26:55+00:00

Here’s a tip that makes some confusions for me:to have the best passing method

  • 0

Here’s a tip that makes some confusions for me:”to have the best passing method pass built-in types by value and other types by reference to constant” now I have some questions below:

1-first of all I think the reason behind suggesting passing by reference to constant is that if the referenced object is not volatile the compiler can convert it to pass by value if it would be more efficient but it would have made more sense to me if there was a special syntax by which we could tell the compiler ok here determine the best passing method yourself I don’t care how it will be but changing pass by reference to constant to pass by value by compiler seems messy somehow to me, in the way it is now if we want the compiler to obey the pass by reference to constant and use a pointer we have to force it to do so since it may determine the more efficient way is to pass by value; so can you lighten me up why it is this way that this is?.

2-Does “pass built-in types by value” include built-in types that are bigger in size than size of a pointer like long double on my platform that has 8 bytes comparing to pointer size that is 4 bytes and does “pass non-built-in types by reference to constant” include things like below that likely can be transfered in memory like built-in types:

struct Test1{
 public:
  char characters[2];
};
struct Test2{
 public:
  char characters[4];
};
  • 1 1 Answer
  • 4 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-18T04:26:56+00:00Added an answer on May 18, 2026 at 4:26 am

    a special syntax by which we could
    tell the compiler ok here determine
    the best passing method yourself

    Boost provides this, as boost::call_traits<T>::param_type.

    why it is this way that this is

    Probably because the rule of thumb works most of the time. If in doubt, you can pass by const reference and it’s unlikely to be horribly inefficient.

    In fact there’s more to the general tip than you say – “small” types which aren’t built-in are also usually best passed by value. For example, the standard library consistently passes iterators by value.

    Does “pass built-in types by value”
    include built-in types that are bigger
    in size than size of a pointer

    Yes. You can reasonably expect a C++ implementation to pass a double or long double by value with acceptable efficiency for almost all practical purposes. If you’ve reached the stage of optimization where this is the lowest-hanging fruit, then you’re beyond general tips. You would measure both in your actual program.

    if the referenced object is not
    volatile the compiler can convert it
    to pass by value if it would be more
    efficient

    Maybe. If the call is successfully inlined, then the compiler can do pretty much anything. If the call target isn’t available to the compiler, then it can’t do it, because in practice a change between value vs. const reference parameter requires changes to both the caller and the callee code. Furthermore, aliasing means that unless the compiler has a lot of information about what’s going on, then even with a non-volatile object it can’t necessarily assume that it won’t change:

    int foo(const int &a, int &b) {
        b = 2;
        return a;
    }
    

    Unless it’s inlined, this code cannot be changed to pass a by value, because someone might call it like this:

    int a = 1;
    std::cout << foo(a,a);
    

    which must print “2”, whereas with a passed by value it would print “1”.

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

Sidebar

Related Questions

Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the problem that I am trying to solve. I have two folders
Here is my code (Say we have a single button on the page that
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is my scenario. I have a website running under AppPool1 and that works
Here's the code I have. It works. The only problem is that the first
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
I have just tried to save a simple *.rtf file with some websites and
Here's the basic setup: I have a thin bar at the top of a

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.