How to declare an array without specifying its size, but with an initializer inside a class in C++?How do I determine the size of my array in C?How to initialize all members of an array to the same value?How do you declare an interface in C++?How do I declare a 2d array in C++ using new?How do I declare and initialize an array in Java?How to extend an existing JavaScript array with another array, without creating a new arrayAdd a new element to an array without specifying the index in BashHow do I use arrays in C++?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsList-initialization of a reference to an array of unknown size: is it supposed to deduce the array size?

Word for giving preference to the oldest child

What Marvel character has this 'W' symbol?

Was Donald Trump at ground zero helping out on 9-11?

Just how much information should you share with a former client?

"Valet parking " or "parking valet"

How can you tell the version of Ubuntu on a system in a .sh (bash) script?

Bouncing map back into its bounds, after user dragged it out

Correct word for a little toy that always stands up?

How do you deal with characters with multiple races?

Embedded C - Most elegant way to insert a delay

How can a circuit not have a neutral?

Can I attune a Circlet of Human Perfection to my animated skeletons to allow them to blend in and speak?

Why put copper in between battery contacts and clamps?

Should students have access to past exams or an exam bank?

What is this kind of symbol meant to be?

Would people understand me speaking German all over Europe?

What are the cons of stateless password generators?

Why are we moving in circles with a tandem kayak?

What are the closest international airports in different countries?

Why don't short runways use ramps for takeoff?

Can living where Earth magnetic ore is abundant provide any protection?

How do discovery writers hibernate?

What is the highest achievable score in Catan

Why are prop blades not shaped like household fan blades?



How to declare an array without specifying its size, but with an initializer inside a class in C++?


How do I determine the size of my array in C?How to initialize all members of an array to the same value?How do you declare an interface in C++?How do I declare a 2d array in C++ using new?How do I declare and initialize an array in Java?How to extend an existing JavaScript array with another array, without creating a new arrayAdd a new element to an array without specifying the index in BashHow do I use arrays in C++?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsList-initialization of a reference to an array of unknown size: is it supposed to deduce the array size?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








8















It is allowed to declare an array without explicitly stating its size, if it has an initializer:



// very fine: decltype(nums) is deduced to be int[3]
int nums[] = 5, 4, 3 ;


However the same doesn't work when the array is declared in a class:



class dummy_class

// incomplete type is not allowed (VS 2019 c++17)
int nums[] = 5, 4, 3 ;
;


Why is this the case?










share|improve this question





















  • 3





    Because it's simply not allowed by the C++ language specification? Perhaps use a std::vector<int> instead?

    – Some programmer dude
    Jul 21 at 11:36











  • Because ISO said so.

    – Ayxan
    Jul 21 at 11:37






  • 1





    @Someprogrammerdude I tried, but my type is non-copyable, (I used int as an example), so I can't use initializer-list-construction at all.

    – Yan B.
    Jul 21 at 11:37

















8















It is allowed to declare an array without explicitly stating its size, if it has an initializer:



// very fine: decltype(nums) is deduced to be int[3]
int nums[] = 5, 4, 3 ;


However the same doesn't work when the array is declared in a class:



class dummy_class

// incomplete type is not allowed (VS 2019 c++17)
int nums[] = 5, 4, 3 ;
;


Why is this the case?










share|improve this question





















  • 3





    Because it's simply not allowed by the C++ language specification? Perhaps use a std::vector<int> instead?

    – Some programmer dude
    Jul 21 at 11:36











  • Because ISO said so.

    – Ayxan
    Jul 21 at 11:37






  • 1





    @Someprogrammerdude I tried, but my type is non-copyable, (I used int as an example), so I can't use initializer-list-construction at all.

    – Yan B.
    Jul 21 at 11:37













8












8








8








It is allowed to declare an array without explicitly stating its size, if it has an initializer:



// very fine: decltype(nums) is deduced to be int[3]
int nums[] = 5, 4, 3 ;


However the same doesn't work when the array is declared in a class:



class dummy_class

// incomplete type is not allowed (VS 2019 c++17)
int nums[] = 5, 4, 3 ;
;


Why is this the case?










share|improve this question
















It is allowed to declare an array without explicitly stating its size, if it has an initializer:



// very fine: decltype(nums) is deduced to be int[3]
int nums[] = 5, 4, 3 ;


However the same doesn't work when the array is declared in a class:



class dummy_class

// incomplete type is not allowed (VS 2019 c++17)
int nums[] = 5, 4, 3 ;
;


Why is this the case?







c++ arrays language-lawyer c++17






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 21 at 13:37









songyuanyao

102k11 gold badges199 silver badges265 bronze badges




102k11 gold badges199 silver badges265 bronze badges










asked Jul 21 at 11:30









Yan B.Yan B.

1,0904 silver badges20 bronze badges




1,0904 silver badges20 bronze badges










  • 3





    Because it's simply not allowed by the C++ language specification? Perhaps use a std::vector<int> instead?

    – Some programmer dude
    Jul 21 at 11:36











  • Because ISO said so.

    – Ayxan
    Jul 21 at 11:37






  • 1





    @Someprogrammerdude I tried, but my type is non-copyable, (I used int as an example), so I can't use initializer-list-construction at all.

    – Yan B.
    Jul 21 at 11:37












  • 3





    Because it's simply not allowed by the C++ language specification? Perhaps use a std::vector<int> instead?

    – Some programmer dude
    Jul 21 at 11:36











  • Because ISO said so.

    – Ayxan
    Jul 21 at 11:37






  • 1





    @Someprogrammerdude I tried, but my type is non-copyable, (I used int as an example), so I can't use initializer-list-construction at all.

    – Yan B.
    Jul 21 at 11:37







3




3





Because it's simply not allowed by the C++ language specification? Perhaps use a std::vector<int> instead?

– Some programmer dude
Jul 21 at 11:36





Because it's simply not allowed by the C++ language specification? Perhaps use a std::vector<int> instead?

– Some programmer dude
Jul 21 at 11:36













Because ISO said so.

– Ayxan
Jul 21 at 11:37





Because ISO said so.

– Ayxan
Jul 21 at 11:37




1




1





@Someprogrammerdude I tried, but my type is non-copyable, (I used int as an example), so I can't use initializer-list-construction at all.

– Yan B.
Jul 21 at 11:37





@Someprogrammerdude I tried, but my type is non-copyable, (I used int as an example), so I can't use initializer-list-construction at all.

– Yan B.
Jul 21 at 11:37












2 Answers
2






active

oldest

votes


















14














This is not allowed because non-static data members might be initialized in different ways (with different sizes), including member initializer list, default member initializer, aggregate initialization, ... But the size of array must be fixed and known at compile-time, which can't be postponed until the initialization. e.g.



class dummy_class

int nums[] = 5, 4, 3 ;
dummy_class(...some_parameters) : nums 5, 4, 3, 2 ()
dummy_class(...some_other_parameters) : nums 5, 4, 3, 2, 1 ()
;





share|improve this answer






















  • 5





    Also aggregate initialization of the class itself :)

    – Rakete1111
    Jul 21 at 11:41



















1














Since it is not allowed, you can do one of these two things:



  • Either use the constructors/methods for initialization along with vector type declaration.

  • Or try making the variable static, but I am afraid that may not be helpful in your case.





share|improve this answer



























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57132784%2fhow-to-declare-an-array-without-specifying-its-size-but-with-an-initializer-ins%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    14














    This is not allowed because non-static data members might be initialized in different ways (with different sizes), including member initializer list, default member initializer, aggregate initialization, ... But the size of array must be fixed and known at compile-time, which can't be postponed until the initialization. e.g.



    class dummy_class

    int nums[] = 5, 4, 3 ;
    dummy_class(...some_parameters) : nums 5, 4, 3, 2 ()
    dummy_class(...some_other_parameters) : nums 5, 4, 3, 2, 1 ()
    ;





    share|improve this answer






















    • 5





      Also aggregate initialization of the class itself :)

      – Rakete1111
      Jul 21 at 11:41
















    14














    This is not allowed because non-static data members might be initialized in different ways (with different sizes), including member initializer list, default member initializer, aggregate initialization, ... But the size of array must be fixed and known at compile-time, which can't be postponed until the initialization. e.g.



    class dummy_class

    int nums[] = 5, 4, 3 ;
    dummy_class(...some_parameters) : nums 5, 4, 3, 2 ()
    dummy_class(...some_other_parameters) : nums 5, 4, 3, 2, 1 ()
    ;





    share|improve this answer






















    • 5





      Also aggregate initialization of the class itself :)

      – Rakete1111
      Jul 21 at 11:41














    14












    14








    14







    This is not allowed because non-static data members might be initialized in different ways (with different sizes), including member initializer list, default member initializer, aggregate initialization, ... But the size of array must be fixed and known at compile-time, which can't be postponed until the initialization. e.g.



    class dummy_class

    int nums[] = 5, 4, 3 ;
    dummy_class(...some_parameters) : nums 5, 4, 3, 2 ()
    dummy_class(...some_other_parameters) : nums 5, 4, 3, 2, 1 ()
    ;





    share|improve this answer















    This is not allowed because non-static data members might be initialized in different ways (with different sizes), including member initializer list, default member initializer, aggregate initialization, ... But the size of array must be fixed and known at compile-time, which can't be postponed until the initialization. e.g.



    class dummy_class

    int nums[] = 5, 4, 3 ;
    dummy_class(...some_parameters) : nums 5, 4, 3, 2 ()
    dummy_class(...some_other_parameters) : nums 5, 4, 3, 2, 1 ()
    ;






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jul 22 at 1:01

























    answered Jul 21 at 11:39









    songyuanyaosongyuanyao

    102k11 gold badges199 silver badges265 bronze badges




    102k11 gold badges199 silver badges265 bronze badges










    • 5





      Also aggregate initialization of the class itself :)

      – Rakete1111
      Jul 21 at 11:41













    • 5





      Also aggregate initialization of the class itself :)

      – Rakete1111
      Jul 21 at 11:41








    5




    5





    Also aggregate initialization of the class itself :)

    – Rakete1111
    Jul 21 at 11:41






    Also aggregate initialization of the class itself :)

    – Rakete1111
    Jul 21 at 11:41














    1














    Since it is not allowed, you can do one of these two things:



    • Either use the constructors/methods for initialization along with vector type declaration.

    • Or try making the variable static, but I am afraid that may not be helpful in your case.





    share|improve this answer





























      1














      Since it is not allowed, you can do one of these two things:



      • Either use the constructors/methods for initialization along with vector type declaration.

      • Or try making the variable static, but I am afraid that may not be helpful in your case.





      share|improve this answer



























        1












        1








        1







        Since it is not allowed, you can do one of these two things:



        • Either use the constructors/methods for initialization along with vector type declaration.

        • Or try making the variable static, but I am afraid that may not be helpful in your case.





        share|improve this answer













        Since it is not allowed, you can do one of these two things:



        • Either use the constructors/methods for initialization along with vector type declaration.

        • Or try making the variable static, but I am afraid that may not be helpful in your case.






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 21 at 11:46









        Shiv KumarShiv Kumar

        193 bronze badges




        193 bronze badges






























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57132784%2fhow-to-declare-an-array-without-specifying-its-size-but-with-an-initializer-ins%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

            Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

            Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?