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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:17:32+00:00 2026-05-28T16:17:32+00:00

I am creating a function like such: void SetItem(const Key &key, const Value &value)

  • 0

I am creating a function like such:

void SetItem(const Key &key, const Value &value)
{
    ...
}

Where Key and Value are some type.

Internally, I want to store the pair like this:

std::pair<const Key &, Value>

So here is my problem:
I need to enforce that Key is actually an l-value so that it doesn’t get cleaned up when the function exits (Unsafe with r-values)

I could make the signature to the function:

void SetItem(Key &key, const Value &value)

Which would prevent the use of r-values, but it then doesn’t allow a const key to be used, which I don’t like either.

Is there a way for me to force Key to be an l-value while preserving the constness?

I am fine with creating an r-value overload to prevent it:

void SetItem(Key &&key, const Value &value)
{
     [What do I put here?]
}

Thanks

  • 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-28T16:17:33+00:00Added an answer on May 28, 2026 at 4:17 pm

    With the improvements from the comments incorporated, it should look like this in a fully conformant C++11 compiler:

    class X{
    public:
      void SetItem(Key const& key, Value const& value);
    private:
      void SetItem(Key const&&, Value const&) = delete;
    };
    

    The private overload will catch all Key rvalues. Access checking isn’t done during overload resolution, as such we can put it under private, and so that possible friends also get a nice error message at compile time, we = delete it.

    For compilers that don’t support explicitly deleted functions yet, you can leave it simply undefined, but that will only show up as a linker error for possible friends. However, the general audience will get the nice “`SetItem’ is inaccessible” compiler error message. 🙂

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

Sidebar

Related Questions

Unless I'm mistaken, creating a function in Python works like this: def my_func(param1, param2):
I'm creating a function in C to convert an index value into a string,
I've got a function creating some XmlDocument: public string CreateOutputXmlString(ICollection<Field> fields) { XmlWriterSettings settings
I'm looking for some Javascript jedi-master to assist in creating a function that adds
I'm reading some tutorials now about jQuery.. function creating,plugin creation etc. but these tutorials
//dom <div id='toBeCloned'><span>Some name</span></div> <div id='targetElm'></div> //js $(function () { //creating a clone var
One such program that uses a wait function like this is this one: #include<stdio.h>
I am creating a console application that would store some sensitive strings in memory,
I'm creating a function where I need to pass an object so that it
I'm creating an initialising function for the class 'Room', and found that the program

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.