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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:39:43+00:00 2026-06-16T02:39:43+00:00

Possible Duplicate: initializer_list and move semantics In this code: #include <vector> #include <initializer_list> template<typename

  • 0

Possible Duplicate:
initializer_list and move semantics

In this code:

#include <vector>
#include <initializer_list>

template<typename T>
class some_custom_container : public std::vector<T>
{
public:
   some_custom_container(const std::initializer_list<T>& contents)
   {
      for (auto& i : contents)
        this->emplace_back(std::move(i));
   }
};

class test_class
{};

int main()
{
    test_class a;

    some_custom_container<test_class> i = { a, test_class(), a };
}

If I’ve understand it, all objects in { a, test_class(), a } are safe-constructed: the named-objects are copied and the unnamed objects are moved to construct the initializer_list. After, this initializer_list is passed by reference to the some_custom_container‘s constructor.

Then, to avoid useless doble-copies I move all of them to fill the vector.

Is it safe this constructor? I mean, in a strange situation, for example if T is evaluated as a reference & or &&, is the vector always well-filled (contains it safe objects)?

If this is the case, why the initializer_list constructor implementations of stl containers aren’t implemented in this way? As I know, their constructors copy and don’t move the contents.

  • 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-16T02:39:44+00:00Added an answer on June 16, 2026 at 2:39 am

    initializer_list only provides const access to its elements. You could use const_cast to make that code compile, but then the moves might end up with undefined behaviour (if the elements of the initializer_list are truly const). So, no it is not safe to do this moving. There are workarounds for this, if you truly need it.

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

Sidebar

Related Questions

Possible Duplicate: c++ why initializer_list behavior for std::vector and std::array are different I defined
Possible Duplicate: Custom Collection Initializers I have a simple Pair class: public class Pair<T1,
Possible Duplicate: Can I list-initialize a vector of move-only type? I use gcc 4.6.1
Possible Duplicate: List<T> readonly with a private set I have a class as: public
Possible Duplicate: Initializer syntax Short code sample to demonstrate (VS2010 SP1, 64-bit Win7): class
Possible Duplicate: Where and why do I have to put “template” and “typename” on
Possible Duplicate: How to initialize a const field in constructor? I have this class:
Possible Duplicate: Learning C++: polymorphism and slicing Array of polymorphic base class objects initialized
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How to do the vector of sets in C++? I want to

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.