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

The Archive Base Latest Questions

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

Code: class A { std::vector<int> x = {2,3}; // x[0] = 2 and x[1]

  • 0

Code:

class A {
  std::vector<int> x = {2,3};                 // x[0] = 2 and x[1] = 3
  std::vector<int> y = std::vector<int>(2,3); // x[0] = 3 and x[1] = 3 Too verbose!!  
};

Is there a way that I can call the constructor of std::vector<int> only using brace initializer, or at least shorter version which gives the same effect?

I don’t want to repeat std::vector<int>.

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

    Is there any hack I could use?

    If your only goal is to not having to “explicitly” specify the type twice you could use decltype to provide some aid in your quest:

    class Obj {
      std::vector<int> v1 = decltype(v1) (2,3);
    };
    

    Also remember that typedef/using is a great way of not having to type1 so much:

    struct Obj {
       using VInt = std::vector<int>;
    // typedef std::vector<int> VInt;
    
       VInt v = VInt (3,2);
    };
    

    1. pun not intended


    What does the standard say about it?

    Sadly the standard says the following about initializing members within the body of your class:

    9.2/5 Class members [class.mem]

    A member can be initialized using a brace-or-equal-initializer. (For
    static data members, see 9.4.2; for non-static data members, see
    12.6.2).

    We’ve already found a little hint about what is and what is not okay to do when initializing members, but to be 100% sure we should continue reading up on what a brace-or-equal-initializer really is.

    8.5/1 Initializers [dcl.init]

    ... brace-or-equal-initializer: = initializer-clause braced-init-list initializer-clause: assignment-expression braced-init-list initializer-list: initializer-clause ...opt initializer-list , initializer-clause ...opt braced-init-list: { initializer-list ,opt } { }

    With the above specification of braced-or-equal-initializer we found that we are faced with two options when initializing members within the body of our class, using either a = together with an initializer-clause, or a braced-init-list on it’s own.

    The above boils down to either of these two:

    struct Obj {
      Type foo = Type (1,2,3); /* example of an initializer-clause */
      Type bar        {1,2,3}; /* example of a  braced-init-list   */
    };
    

    braced-init-list looks awesome, let’s use it!

    Since std::vector<...> accepts a std::initializer_list in one overload of it’s constructors we cannot use a braced-init-list to invoke the constructor taking two arguments (size_type count, const T& value), because that will instead be used as the contents of our vector.

    We are therefore stuck with using a initializer-clause.

    See the previous hack for a confirming, but maybe not so obvious, solution.

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

Sidebar

Related Questions

I'm designing a class that has a std::vector<int> as an instance variable. I'm using
When trying to compile this code: #include <iostream> #include <vector> using namespace std; class
I've recently been working with code that looks like this: using namespace std; class
Example code: #include <cstdlib> #include <iostream> using namespace std; class A { public: A(int
I have the following code #include <iostream> #include <vector> using namespace std; int distance(vector<int>&
I wrote the following code: class MyObjectHolder { public: std::vector<int> getMyObject() const { return
I wrote this little code std::map<int,template<class T>> map_; map_.insert(make_pair<int,message>(myMsg.id,myMsg)); but the compiler doesn't seem
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
I have the following code compiled by gcc: #include <iostream> using namespace std; class
The following C++ code gives an error while compiling: #include<iostream> using namespace std; class

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.