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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:03:01+00:00 2026-06-14T09:03:01+00:00

In a visitor context, I need to temporarily set a variable before visiting children,

  • 0

In a visitor context, I need to temporarily set a variable before visiting children, and revert that variable afterward. I’m using the following code, but I’m sure there’s a more elegant and proper way to do this:

template <typename TYPE> class TemporaryAssignment {
protected:
    TYPE& mVariable;
    TYPE mOriginalValue;
public:
    TemporaryAssignment(TYPE& inVariable, TYPE inValue) 
        : mVariable(inVariable), mOriginalValue(inVariable) {
        mVariable = inValue;
    }
    ~TemporaryAssignment(void) {
        mVariable = mOriginalValue;
    }
};

This allows me to write something like the following:

{
    ...
    TemporaryAssignment<string> t(myVariable, myTemporaryValue);
    visitChildren();
    ...
}
// previous value of myVariable is restored

The variable will revert to its previous value when the temporary assignment object goes out of scope. What is a better way to do this?

  • 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-14T09:03:02+00:00Added an answer on June 14, 2026 at 9:03 am

    Looks OK to me except that the destructor can throw, which is bad. swap with the original value instead of assigning (edit: this deals with std::string, but see the comments for possible problems with classes that are less user-friendly than string).

    If you back off a little from this part of the code, perhaps you can find a way to not need to set a temporary value at all. Shared mutable state in an object can be bad for the same reason that mutable globals are bad, but to a lesser extent because it only messes up your class instead of messing up your whole program.

    For example perhaps you could copy the whole object, set a new value for the variable and visit the copy instead of visiting yourself. Obviously that’s not necessarily possible or efficient, you have to look for alternatives on a case by case basis. Maybe the copy can be shallow as far as the children are concerned (i.e. refer to the same child objects), that might be sufficient to make it cheap.

    Regarding usage, you could deduce the type like this (untested code):

    template <typename T, typename ARG>
    TemporaryAssignment<T> temp_value(T &var, ARG &&newvalue) {
        return TemporaryAssignment(var, std::forward<ARG>(newValue));
    }
    

    Usage:

    auto t = temp_value(myVariable, myTemporaryValue);
    

    Then you need a move constructor for TemporaryAssignment:

    template <typename TYPE> class TemporaryAssignment {
        // change data member
        TYPE *mVariable;
        TYPE mOriginalValue;
    public:
        TemporaryAssignment(TYPE &inVariable, TYPE inValue) 
        : mVariable(&inVariable), mOriginalValue(std::move(inVariable)) {
            *mVariable = std::move(inValue);
        }
        TemporaryAssignment(TemporaryAssignment &&rhs) {
            mOriginalValue = std::move(rhs.mOriginalValue);
            mVariable = rhs.mVariable;
            rhs.mVariable = 0;
        }
        ~TypeAssignment() {
            using std::swap;
            if (mVariable) {
                swap(*mVariable, mOriginalValue);
            }
        }
        // can't remember whether this is needed
        TemporaryAssignment(const TemporaryAssignment &) = delete;
        TemporaryAssignment &operator=(const TemporaryAssignment &) = delete;
        TemporaryAssignment &operator=(TemporaryAssignment &&) = delete;
    };
    

    I thought a bit about specifying operator= for TemporaryAssignment so as to make the usage look like an assignment, but I didn’t come up with anything good.

    auto t = (temporary(myVariable) = myTemporaryValue);
    

    is plausible, but you probably don’t want TemporaryAssignment to have operator= defined because the meaning of:

    t = otherTemporaryValue;
    

    isn’t necessarily clear and probably shouldn’t be allowed. Maybe with a second class to be returned from temporary, and that returns TemporaryAssignment from its operator=.

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

Sidebar

Related Questions

I am using the following code to output a block of content without a
I have a visitor table that looks like this: VID Name Address DOB 001
The items that a visitor has in his shopping bag are linked to a
I have a jQuery colorpicker that the visitor can change and I am trying
How can I gather the visitor's time zone information? I need both: the time
How can I make a visitor's browser go fullscreen using JavaScript, in a way
We're building a set of external web services to be consumed client-side (using jquery/AJAX)
I need to check if a user (visitor) liked my Facebook page ( a
I need to do the following and I'm not sure if this is done
I am using jQuery to get visitor's location via their IP address. There's 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.