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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:05:44+00:00 2026-05-31T05:05:44+00:00

While searching a solution for calling a function that accepts both lvalue and rvalue

  • 0

While searching a solution for calling a function that accepts both lvalue and rvalue references I wrote the following piece of code (no, I will never do that again, I promise).

The goal was providing a function that both accepts lvalue and rvalue references, I did not want to write two functions or to really care about lvalue/rvalue on the caller’s side. Therefore, I thought of providing some macro/function that wraps a parameter so it can be passed whether it’s an l/rvalue – in this case get_addr.

In no case I would want to change data passed to the function.

template<typename T>
std::shared_ptr<T> get_addr(T t) {
    std::shared_ptr<T> p(new T(t));
    return p;
}

int get_value(int x){
    return x * 2;
}

void print_value(int* p){
    cout << *p << endl;
}

int main() {
    print_value(get_addr(get_value(42)).get());
}

While that code is working fine for me, I doubt it is safe. My guess: after calling get() from shared_ptr, the result of get_value() is not necessary anymore and could get dismissed. So, print_value could receive an invalid reference. Does that make sense? In any case, I do not like my code provided. I solved the issue by using a temporary variable.

Nevertheless, I’m still curious if there was any better solution for that problem. From the syntax’ point of view, I’d like best only one function “void print_value(int&& p)” that accepts both l/rvalues. I’ve looked at move semantics, l/rvalue conversions but didn’t find a solution that really looks nice.

  • 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-31T05:05:45+00:00Added an answer on May 31, 2026 at 5:05 am

    In no case I would want to change data passed to the function.

    What’s wrong with this then?

    void print_value(int p){
        cout << p << endl;
    }
    

    Or the following, if you don’t want to copy some expensive type.

    void print_value(T const& p){
        cout << p << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i was searching for a while, but couldn't find a fitting solution: My Case:
While searching a bug in my code today I found a strange thing. When
While searching for some functions in C++ standard library documentation I read that push
I'm searching a while for this and I can't found something that works for
I have been searching for a solution to this for a while and have
While searching for the solution, I've come across various links which talk about reading
I've been searching for this solution for a while now... [bla bla... google.. bla]...
I've been searching for a while and still can't find a simple solution to
While searching the interweb for a solution for my VB.net problems I often find
I found similar questions while searching for a solution for my problem but 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.