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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:38:29+00:00 2026-06-17T11:38:29+00:00

I have a factory function which return a stl container: const std::vector<int> f(…) {

  • 0

I have a factory function which return a stl container:

const std::vector<int> f(...) {
    std::vector<int> retval;
    return retval;
}

I guess it is ok to define a stl instance as follow(no error):

const std::vector<int> stl_instance(f(...));

But is it efficient to do so?

Is the temporary stl object directly assigned to stl_instance?

  • 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-17T11:38:30+00:00Added an answer on June 17, 2026 at 11:38 am

    Returning a const rvalue is an anti-pattern in C++11. First consider returning non-const rvalues:

    std::vector<int> f(int n) 
    { 
        return std::vector<int>(n); 
    }
    
    int main() 
    { 
        std::vector<int> v; 
        v = f(3); 
    } 
    

    In C++98/03, this code will go to the heap at least twice:

    1. To create the vector inside of f (if the RVO applies)
    2. To assign from f’s return to v.

    If your compiler does not apply the RVO, you get 3 heap allocations.

    In C++11, you only get 1 heap allocation: to create the vector inside of f. This happens regardless of the RVO. The reason is that all the STL containers have move constructors and move assignment operators that have the signatures

    vector( vector&& other );
    vector& operator=( vector&& other );
    

    The rvalue references && will move the resources from inside your creation function directly to their destination. However, your code has the signature

    const std::vector<int> f(int n) 
    { 
        return std::vector<int>(n); 
    }
    

    will disable the move semantics because a T&& (i.e. the argument of the move constructor and assignment operator) will not bind to a const rvalue parameter (i.e. the return value of your function). This effectively makes your code run as under C++98/03 (i.e. with 2 or 3 heap allocations).

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

Sidebar

Related Questions

I have a angularjs service: angular.module('myApp.services', ['ngResource']) .factory('Scheduler', function ($resource) { return $resource('/api/scheduler/:id', {id:'@id'},
I have a factory for creating containers based on ugly containers template<class T> std::vector<T>
Suppose I have some kind of factory function which creates objects that are largely
I'm learning JQuery and have found the factory function $() with its selectors quite
I have a class with a factory-pattern function in it: abstract class ParentObj {
I have the following code $.post( /factory/set,{ key : value }, function(response) { });
I have this factory: FactoryGirl.define do factory :from_sector, class: Sector do sequence(:name) { |n|
I have a Factory like this: FactoryGirl.define do factory :subject do name 'Calculus' end
I have a factory class, DocumentLoaderFactory , which simply returns an instance that implements
In classic ASP (which I am forced to use), I have a few factory

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.