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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:33:16+00:00 2026-06-15T19:33:16+00:00

Say we have an utility function: std::string GetDescription() { return The description.; } Is

  • 0

Say we have an utility function:

std::string GetDescription() { return "The description."; }

Is it OK to return the string literal? Is the implicitly created std::string object copied?

I thought about always returning it like this:

std::string GetDescription() { return std::move(std::string("The description.")); }

But it’s of course longer and more verbose. We could also assume that compiler RVO will help us a bit

std::string GetDescription() { return std::string("The description."); }

Yet still, I don’t know what it really has to do, instead of what can it do.

  • 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-15T19:33:18+00:00Added an answer on June 15, 2026 at 7:33 pm
    std::string GetDescription() { return "XYZ"; }
    

    is equivalent to this:

    std::string GetDescription() { return std::string("XYZ"); }
    

    which in turn is equivalent to this:

    std::string GetDescription() { return std::move(std::string("XYZ")); }
    

    Means when you return std::string("XYZ") which is a temporary object, then std::move is unnecessary, because the object will be moved anyway (implicitly).

    Likewise, when you return "XYZ", then the explicit construction std::string("XYZ") is unnecessary, because the construction will happen anyway (implicitly).


    So the answer to this question:

    Is the implicitly created std::string object copied?

    is NO. The implicitly created object is after all a temporary object which is moved (implicitly). But then the move can be elided by the compiler!

    So the bottomline is this : you can write this code and be happy:

    std::string GetDescription() { return "XYZ"; }
    

    And in some corner-cases, return tempObj is more efficient (and thus better) than return std::move(tempObj).

    • 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 a utility function that, for simplicity's sake (the real thing
Lets say I have a few utility functions in file tests/utils/functions.js. I would like
In UNIX, I have a utility, say 'Test_Ex', a binary file. How can I
Let's say you are writing a library and you have a bunch of utility
Say I have a library or service which is created by an application, and
(When I say object address, I mean the string that you type in Python
Say you have an object that has a kind of connection to a platform
I have class say for example public class Item { int price; String name;
Let's say I have a utility class DateUtil (see below). To use this method
I have a polymorphic function like: convert :: (Show a) => a -> String

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.