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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:42:00+00:00 2026-06-06T12:42:00+00:00

Let’s say we have a class that embed another parametric one, that has to

  • 0

Let’s say we have a class that embed another parametric one, that has to support both copy and move.
And let’s assume that -in certain circumstance- there is the need to get out the inner value somehow.

A typical approach can be this one:

template<class T>
class wrapper
{
public:
    wrapper() :val() {}
    wrapper(T s) :val(std::move(s)) {}

    wrapper(const wrapper& s) :val(s.val) {}
    wrapper(wrapper&& s) :val(std::move(s.val)) {}
    wrapper& operator=(wrapper s) { val = std::move(s.val); return *this; }

    T value() const { return val; }
private:
    T val;
};

That’s not the only way of doing it, and may be there is no need to expliciate copy and move, but let them be.

The point is another: Suppose T -in ceratin instances- is itself a copy/movable class.
Of course, wrapper::value() returns a copy of T.

Now suppose the returned T has to go as a parameter into another call, and that the containing wrapper
is temporary.

The simpler simulation is

calledfn(wrapper<T>(someT).value());

Again: done in this way is clueless, but more complex cases require this trivial one to work.

In theory we can admit to move away val from its temporary wrapper to give it to the caller, but…
what signature should have the value() method so that it is bound when wrapper<T> is temporary but is not bound when it is not temporary, for which value() const should be preferred?

  • 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-06T12:42:01+00:00Added an answer on June 6, 2026 at 12:42 pm

    In C++11 you can overload methods on wether or not the this-pointer is a rvalue:

    T value() &&; //Will only be called if the object is a mutable rvalue
    T value() const &; //Will only be called if the object is an lvalue
    

    Note that according to 13.1.2 [over.load]in the standard if any overload has a reference specifier (lvalue or rvalue) all have to get one. So The overload for the non-temporary case has to be const &. I’m not completely sure wether or not this will bind to a const r-value, so you might need a const && overload to catch that case.

    I don’t know about the compiler support for this feature, so this might be somewhat theoretical if the support isn’t there yet. In that case you might work around by creating a move_value method and using a free function which dispatches to the correct method:

    template<class T> class wrapper {
        ...    
        T value() const;
        T move_value();    
    };
    template<typename T> T value(wrapper<T>&& self) { return std::move(self.move_value(); }
    template<typename T> T value(const wrapper<T>& self) { return std::move(self.value(); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have one class User, and it has a property of type
Let's say you have a class library project that has any number of supplemental
Let's say we have 3 classes: A, B and C. Each class has the
Let's say I have an abstract parent class called shape, and that there are
Let's say we have a table that has 2 million rows. It has two
Let's say I have the following classes : public class MyProductCode { private String
Let's say you have a method that expects a numerical value as an argument.
Let's say I have a bunch of links that share a click event: <a
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have a C++ Visual Studio 2010 solution with 2 projects: one

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.