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

  • Home
  • SEARCH
  • 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 8876647
In Process

The Archive Base Latest Questions

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

What are the differences between the following three initializations with std::initializer_list s? std::vector<int> a{

  • 0

What are the differences between the following three initializations with std::initializer_lists?

std::vector<int> a{ 2, 3, 5, 7};
std::vector<int> b( { 2, 3, 5, 7} );
std::vector<int> c = { 2, 3, 5, 7};

In the above example, std::vector is just a placeholder, but I am interested in a general answer.

  • 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-14T19:15:48+00:00Added an answer on June 14, 2026 at 7:15 pm

    Let’s abstract away from std::vector. And call it T.

    T t{a, b, c};
    T t = { a, b, c };
    T t({a, b, c});
    

    The first two forms are list initialization (and the only difference between them is that if T is a class, for the second explicit constructors are forbidden to be called. If one is called, the program becomes ill-formed). The last form is just ordinary direct initialization as we know it from C++03:

    T t(arg);
    

    That there appears a {a, b, c} as arg means that the argument for the constructor call is a brace initializer list. This third form does not have the special handling that list initialization has. T must be a class type there, even if the braced init list has only 1 argument. I’m glad that we put clear rules before releasing C++11 in this case.


    As in terms of what constructors are called for the third, let’s assume

    struct T {
      T(int);
      T(std::initializer_list<int>);
    };
    
    T t({1});
    

    Since a direct initialization is just a call to the overloaded constructors, we can transform this to

    void ctor(int); 
    void ctor(std::initializer_list<int>);
    void ctor(T const&);
    void ctor(T &&);
    

    We can use both trailing functions, but we would need a user defined conversion if we picked these functions. To initialize the T ref parameter, list initialization will be used because this is not a direct initialization with parens (so the parameter initialization is equivalent to T ref t = { 1 }). The first two functions are exact matches. However, the Standard says that in such a case, when one function converts to std::initializer_list<T> and the other does not, then the former function wins. Therefor in this scenario, the second ctor would be used. Note that in this scenario, we will not do two-phase overload resolution with first only initializer list ctors – only list initialization will do that.


    For the first two, we will use list-initialization, and it will do context dependent things. If T is an array, it will initialize an array. Take this example for a class

    struct T {
      T(long);
      T(std::initializer_list<int>);
    };
    
    T t = { 1L };
    

    In this case, we do two-phase overload resolution. We first only consider initializer list constructors and see if one matches, as argument we take the whole braced init list. The second ctor matches, so we pick it. We will ignore the first constructor. If we have no initializer list ctor or if none matches, we take all ctors and the elements of the initializer list

    struct T {
      T(long);
    
      template<typename A = std::initializer_list<int>>
      T(A);
    };
    
    T t = { 1L };
    

    In this case we pick the first constructor, because 1L cannot be converted to std::initializer_list<int>.

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

Sidebar

Related Questions

Are there any differences when it comes to performance between the following three border
Very specific question (I hope): What are the differences between the following three codes?
What are the differences between the following three signatures? static <T> void foo(List<T>, Comparator<?
What is the difference between the following three annotations: [ScaffoldColumn(false)] [Display(Name = )] [Display(AutoGenerateField=false)]
In VB.Net is there any difference between the following three ways of initialising object
Are there any differences between the following two ways of field initialization? When to
What's the difference between the following three .htaccess rules and when to use each,
What is the difference between the following three concepts? a JDBC connection an H2
Is there any differences between following code snippets? I'm using VS 2010, .NET 4,
In android,Is there any differences between the following code? new Intent(this, MyOtherActivity.class); new Intent(Context.this,

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.