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;
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
add a comment |
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
3
Because it's simply not allowed by the C++ language specification? Perhaps use astd::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 usedint
as an example), so I can't use initializer-list-construction at all.
– Yan B.
Jul 21 at 11:37
add a comment |
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
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
c++ arrays language-lawyer c++17
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 astd::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 usedint
as an example), so I can't use initializer-list-construction at all.
– Yan B.
Jul 21 at 11:37
add a comment |
3
Because it's simply not allowed by the C++ language specification? Perhaps use astd::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 usedint
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
add a comment |
2 Answers
2
active
oldest
votes
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 ()
;
5
Also aggregate initialization of the class itself :)
– Rakete1111
Jul 21 at 11:41
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 ()
;
5
Also aggregate initialization of the class itself :)
– Rakete1111
Jul 21 at 11:41
add a comment |
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 ()
;
5
Also aggregate initialization of the class itself :)
– Rakete1111
Jul 21 at 11:41
add a comment |
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 ()
;
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 ()
;
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jul 21 at 11:46
Shiv KumarShiv Kumar
193 bronze badges
193 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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