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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:50:06+00:00 2026-05-28T22:50:06+00:00

Sometimes we like to take a large parameter by reference, and also to make

  • 0

Sometimes we like to take a large parameter by reference, and also to make the reference const if possible to advertize that it is an input parameter. But by making the reference const, the compiler then allows itself to convert data if it’s of the wrong type. This means it’s not as efficient, but more worrying is the fact that I think I am referring to the original data; perhaps I will take it’s address, not realizing that I am, in effect, taking the address of a temporary.

The call to bar in this code fails. This is desirable, because the reference is not of the correct type. The call to bar_const is also of the wrong type, but it silently compiles. This is undesirable for me.

#include<vector>
using namespace std;

int vi;

void foo(int &) { }
void bar(long &) { }
void bar_const(const long &) { }

int main() {
   foo(vi);
   // bar(vi); // compiler error, as expected/desired
   bar_const(vi);
}

What’s the safest way to pass a lightweight, read-only reference? I’m tempted to create a new reference-like template.

(Obviously, int and long are very small types. But I have been caught out with larger structures which can be converted to each other. I don’t want this to silently happen when I’m taking a const reference. Sometimes, marking the constructors as explicit helps, but that is not ideal)

Update: I imagine a system like the following: Imagine having two functions X byVal(); and X& byRef(); and the following block of code:

 X x;
 const_lvalue_ref<X> a = x; // I want this to compile
 const_lvalue_ref<X> b = byVal(); // I want this to fail at compile time
 const_lvalue_ref<X> c = byRef(); // I want this to compile

That example is based on local variables, but I want it to also work with parameters. I want to get some sort of error message if I’m accidentally passing a ref-to-temporary or a ref-to-a-copy when I think I’ll passing something lightweight such as a ref-to-lvalue. This is just a ‘coding standard’ thing – if I actually want to allow passing a ref to a temporary, then I’ll use a straightforward const X&. (I’m finding this piece on Boost’s FOREACH to be quite useful.)

  • 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-28T22:50:07+00:00Added an answer on May 28, 2026 at 10:50 pm

    (Answering my own question thanks to this great answer on another question I asked. Thanks @hvd.)

    In short, marking a function parameter as volatile means that it cannot be bound to an rvalue. (Can anybody nail down a standard quote for that? Temporaries can be bound to const&, but not to const volatile & apparently. This is what I get on g++-4.6.1. (Extra: see this extended comment stream for some gory details that are way over my head 🙂 ))

    void foo( const volatile Input & input, Output & output) {
    }
    
    foo(input, output); // compiles. good
    foo(get_input_as_value(), output); // compile failure, as desired.
    

    But, you don’t actually want the parameters to be volatile. So I’ve written a small wrapper to const_cast the volatile away. So the signature of foo becomes this instead:

    void foo( const_lvalue<Input> input, Output & output) {
    }
    

    where the wrapper is:

    template<typename T>
    struct const_lvalue {
        const T * t;
        const_lvalue(const volatile T & t_) : t(const_cast<const T*>(&t_)) {}
        const T* operator-> () const { return t; }
    };
    

    This can be created from an lvalue only

    Any downsides? It might mean that I accidentally misuse an object that is truly volatile, but then again I’ve never used volatile before in my life. So this is the right solution for me, I think.

    I hope to get in the habit of doing this with all suitable parameters by default.

    Demo on ideone

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

Sidebar

Related Questions

I'd like to take user input (sometimes this will be large paragraphs) and generate
I am not an idiot, but header files make me feel like one sometimes.
I have a template class that can (and sometimes has to) take a const
I often find that attributes can be too large. Sometimes it feels like the
MonotouchDialog makes it very easy to create UITableView Dialogs, but sometimes questions like that
I would like to repeatedly execute a subprocess as fast as possible. However, sometimes
I'm using the the C++/Qt print function qDebug, but sometimes I would like to
I would like to expose a function that can take an optional anonymous method
My issue is that sometimes a piece of JavaScript (often Google Analytics) may take
I find that local builds will sometimes take 5-10 minutes of an application we

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.