Statically initializing constexpr std::array of objects containing function pointers Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag? The Ask Question Wizard is Live!Initializing a static std::map<int, int> in C++C++11 Proper Structure InitializationWhy should I use a pointer rather than the object itself?C++14 constexpr static const std::array initializationgcc doesn't accept pack expansion in default template argumentSFINAE constexpr with std::getHow to return a static const int std::array from a method?g++ 8.1 template deduction ambiguity with std flag equal to 'c++17'Question about alias declarations in template classPass reference to a static storage cstyle array from on constexpr function to another constexpr function
At the end of Thor: Ragnarok why don't the Asgardians turn and head for the Bifrost as per their original plan?
Why light coming from distant stars is not discreet?
Apollo command module space walk?
What's the purpose of writing one's academic biography in the third person?
Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?
Storing hydrofluoric acid before the invention of plastics
Echoing a tail command produces unexpected output?
What causes the vertical darker bands in my photo?
Coloring maths inside a tcolorbox
What is the role of the transistor and diode in a soft start circuit?
The logistics of corpse disposal
Sci-Fi book where patients in a coma ward all live in a subconscious world linked together
ListPlot join points by nearest neighbor rather than order
Why was the term "discrete" used in discrete logarithm?
How to find out what spells would be useless to a blind NPC spellcaster?
Identify plant with long narrow paired leaves and reddish stems
Is the Standard Deduction better than Itemized when both are the same amount?
What is the logic behind the Maharil's explanation of why we don't say שעשה ניסים on Pesach?
Seeking colloquialism for “just because”
Why do we bend a book to keep it straight?
Single word antonym of "flightless"
Generate an RGB colour grid
How does the particle を relate to the verb 行く in the structure「A を + B に行く」?
Bete Noir -- no dairy
Statically initializing constexpr std::array of objects containing function pointers
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?
The Ask Question Wizard is Live!Initializing a static std::map<int, int> in C++C++11 Proper Structure InitializationWhy should I use a pointer rather than the object itself?C++14 constexpr static const std::array initializationgcc doesn't accept pack expansion in default template argumentSFINAE constexpr with std::getHow to return a static const int std::array from a method?g++ 8.1 template deduction ambiguity with std flag equal to 'c++17'Question about alias declarations in template classPass reference to a static storage cstyle array from on constexpr function to another constexpr function
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to statically initialize a constexpr std::array of objects containing function pointers with the following code:
#include <array>
using TVoidVoid = void(*)(void);
class State
public:
constexpr State(TVoidVoid function) : function_function
private:
TVoidVoid function_;
;
void OnEvent1()
void OnEvent2()
constexpr std::array<State, 10> states = OnEvent1, OnEvent2;
int main()
I am compiling with:
g++ -Wall -Wextra -Wshadow -Weffc++ -Wstrict-aliasing -ansi -pedantic -Werror -std=c++14 main.cpp
I have trouble understanding the compiling error I'm getting:
main.cpp:14:69: error: too many initializers for ‘const std::array<State, 10>’
constexpr std::array<State, 10> states = OnEvent1, OnEvent2
The compiler is g++ (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0.
What could be the problem here? Many thanks in advance!
c++ c++14
add a comment |
I am trying to statically initialize a constexpr std::array of objects containing function pointers with the following code:
#include <array>
using TVoidVoid = void(*)(void);
class State
public:
constexpr State(TVoidVoid function) : function_function
private:
TVoidVoid function_;
;
void OnEvent1()
void OnEvent2()
constexpr std::array<State, 10> states = OnEvent1, OnEvent2;
int main()
I am compiling with:
g++ -Wall -Wextra -Wshadow -Weffc++ -Wstrict-aliasing -ansi -pedantic -Werror -std=c++14 main.cpp
I have trouble understanding the compiling error I'm getting:
main.cpp:14:69: error: too many initializers for ‘const std::array<State, 10>’
constexpr std::array<State, 10> states = OnEvent1, OnEvent2
The compiler is g++ (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0.
What could be the problem here? Many thanks in advance!
c++ c++14
add a comment |
I am trying to statically initialize a constexpr std::array of objects containing function pointers with the following code:
#include <array>
using TVoidVoid = void(*)(void);
class State
public:
constexpr State(TVoidVoid function) : function_function
private:
TVoidVoid function_;
;
void OnEvent1()
void OnEvent2()
constexpr std::array<State, 10> states = OnEvent1, OnEvent2;
int main()
I am compiling with:
g++ -Wall -Wextra -Wshadow -Weffc++ -Wstrict-aliasing -ansi -pedantic -Werror -std=c++14 main.cpp
I have trouble understanding the compiling error I'm getting:
main.cpp:14:69: error: too many initializers for ‘const std::array<State, 10>’
constexpr std::array<State, 10> states = OnEvent1, OnEvent2
The compiler is g++ (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0.
What could be the problem here? Many thanks in advance!
c++ c++14
I am trying to statically initialize a constexpr std::array of objects containing function pointers with the following code:
#include <array>
using TVoidVoid = void(*)(void);
class State
public:
constexpr State(TVoidVoid function) : function_function
private:
TVoidVoid function_;
;
void OnEvent1()
void OnEvent2()
constexpr std::array<State, 10> states = OnEvent1, OnEvent2;
int main()
I am compiling with:
g++ -Wall -Wextra -Wshadow -Weffc++ -Wstrict-aliasing -ansi -pedantic -Werror -std=c++14 main.cpp
I have trouble understanding the compiling error I'm getting:
main.cpp:14:69: error: too many initializers for ‘const std::array<State, 10>’
constexpr std::array<State, 10> states = OnEvent1, OnEvent2
The compiler is g++ (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0.
What could be the problem here? Many thanks in advance!
c++ c++14
c++ c++14
edited Mar 8 at 17:55
Mihai Galos
asked Mar 8 at 17:42
Mihai GalosMihai Galos
5251520
5251520
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You need a default constructor (for the last 8)
#include <array>
using TVoidVoid = void(*)(void);
class State
public:
// This static is equivalent to a TVoidVoid
// used by the default constructor
static void DefFunct()
constexpr State(TVoidVoid function) : function_function
// We create a default constructor for the
// empty elemnts of the array with our function
constexpr State() : function_(DefFunct)
private:
TVoidVoid function_;
;
void OnEvent1()
void OnEvent2()
constexpr std::array<State, 10> states = OnEvent1, OnEvent2;
int main()
1
Thanks Mirko, I reformulated the question, but the code remains the same: I need an array of objects containing function pointers. I would like to construct these objects of type State in-place in the array.
– Mihai Galos
Mar 8 at 17:57
@MihaiGalos Edited the answer.
– Mirko
Mar 8 at 18:05
1
So the remaining items need to be default-constructed, which was producing the error. Of course. All elements are statically constructed. Awesome. Please remove the first answer and leave the one after the edit, perhaps add a few comments why this is needed. Then we can close this question! Many thanks!
– Mihai Galos
Mar 8 at 18:13
add a comment |
The error message could be better. But what's tripping the initialization is in fact that you don't have enough braces. Recall that a std::array
is an aggregate wrapping a raw array. So you need to initialize like this:
constexpr std::array<State, 10> states = OnEvent1, OnEvent2 ;
Otherwise, the somewhat inaccurate brace ellision detection algorithm assumes OnEvent1
is to initialize the internal array, and the second clause is redundant.
Now you just need to provide a default c'tor for State
, or adjust the array size.
thank you! I did not know I needed a default constructor for the the non-default initialized elements.
– Mihai Galos
Mar 8 at 18:22
1
@Mihai - Each c'tor is a different way to initialize objects of your class. The first two don't need it. It's the rest. You can also provide it in another way.TVoidVoid function = nullptr
will make your existing c'tor usable as a default c'tor too.
– StoryTeller
Mar 8 at 18:26
1
I'm a bit amazed that with extra (StoryTeller answer) and with less works too (my answer), but with the original quantity it did not.
– Mirko
Mar 8 at 18:26
1
@Mirko - Initialization in C++ is crazy, sadly.
– StoryTeller
Mar 8 at 18:27
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%2f55068356%2fstatically-initializing-constexpr-stdarray-of-objects-containing-function-poin%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
You need a default constructor (for the last 8)
#include <array>
using TVoidVoid = void(*)(void);
class State
public:
// This static is equivalent to a TVoidVoid
// used by the default constructor
static void DefFunct()
constexpr State(TVoidVoid function) : function_function
// We create a default constructor for the
// empty elemnts of the array with our function
constexpr State() : function_(DefFunct)
private:
TVoidVoid function_;
;
void OnEvent1()
void OnEvent2()
constexpr std::array<State, 10> states = OnEvent1, OnEvent2;
int main()
1
Thanks Mirko, I reformulated the question, but the code remains the same: I need an array of objects containing function pointers. I would like to construct these objects of type State in-place in the array.
– Mihai Galos
Mar 8 at 17:57
@MihaiGalos Edited the answer.
– Mirko
Mar 8 at 18:05
1
So the remaining items need to be default-constructed, which was producing the error. Of course. All elements are statically constructed. Awesome. Please remove the first answer and leave the one after the edit, perhaps add a few comments why this is needed. Then we can close this question! Many thanks!
– Mihai Galos
Mar 8 at 18:13
add a comment |
You need a default constructor (for the last 8)
#include <array>
using TVoidVoid = void(*)(void);
class State
public:
// This static is equivalent to a TVoidVoid
// used by the default constructor
static void DefFunct()
constexpr State(TVoidVoid function) : function_function
// We create a default constructor for the
// empty elemnts of the array with our function
constexpr State() : function_(DefFunct)
private:
TVoidVoid function_;
;
void OnEvent1()
void OnEvent2()
constexpr std::array<State, 10> states = OnEvent1, OnEvent2;
int main()
1
Thanks Mirko, I reformulated the question, but the code remains the same: I need an array of objects containing function pointers. I would like to construct these objects of type State in-place in the array.
– Mihai Galos
Mar 8 at 17:57
@MihaiGalos Edited the answer.
– Mirko
Mar 8 at 18:05
1
So the remaining items need to be default-constructed, which was producing the error. Of course. All elements are statically constructed. Awesome. Please remove the first answer and leave the one after the edit, perhaps add a few comments why this is needed. Then we can close this question! Many thanks!
– Mihai Galos
Mar 8 at 18:13
add a comment |
You need a default constructor (for the last 8)
#include <array>
using TVoidVoid = void(*)(void);
class State
public:
// This static is equivalent to a TVoidVoid
// used by the default constructor
static void DefFunct()
constexpr State(TVoidVoid function) : function_function
// We create a default constructor for the
// empty elemnts of the array with our function
constexpr State() : function_(DefFunct)
private:
TVoidVoid function_;
;
void OnEvent1()
void OnEvent2()
constexpr std::array<State, 10> states = OnEvent1, OnEvent2;
int main()
You need a default constructor (for the last 8)
#include <array>
using TVoidVoid = void(*)(void);
class State
public:
// This static is equivalent to a TVoidVoid
// used by the default constructor
static void DefFunct()
constexpr State(TVoidVoid function) : function_function
// We create a default constructor for the
// empty elemnts of the array with our function
constexpr State() : function_(DefFunct)
private:
TVoidVoid function_;
;
void OnEvent1()
void OnEvent2()
constexpr std::array<State, 10> states = OnEvent1, OnEvent2;
int main()
edited Mar 8 at 18:21
answered Mar 8 at 17:53
MirkoMirko
524210
524210
1
Thanks Mirko, I reformulated the question, but the code remains the same: I need an array of objects containing function pointers. I would like to construct these objects of type State in-place in the array.
– Mihai Galos
Mar 8 at 17:57
@MihaiGalos Edited the answer.
– Mirko
Mar 8 at 18:05
1
So the remaining items need to be default-constructed, which was producing the error. Of course. All elements are statically constructed. Awesome. Please remove the first answer and leave the one after the edit, perhaps add a few comments why this is needed. Then we can close this question! Many thanks!
– Mihai Galos
Mar 8 at 18:13
add a comment |
1
Thanks Mirko, I reformulated the question, but the code remains the same: I need an array of objects containing function pointers. I would like to construct these objects of type State in-place in the array.
– Mihai Galos
Mar 8 at 17:57
@MihaiGalos Edited the answer.
– Mirko
Mar 8 at 18:05
1
So the remaining items need to be default-constructed, which was producing the error. Of course. All elements are statically constructed. Awesome. Please remove the first answer and leave the one after the edit, perhaps add a few comments why this is needed. Then we can close this question! Many thanks!
– Mihai Galos
Mar 8 at 18:13
1
1
Thanks Mirko, I reformulated the question, but the code remains the same: I need an array of objects containing function pointers. I would like to construct these objects of type State in-place in the array.
– Mihai Galos
Mar 8 at 17:57
Thanks Mirko, I reformulated the question, but the code remains the same: I need an array of objects containing function pointers. I would like to construct these objects of type State in-place in the array.
– Mihai Galos
Mar 8 at 17:57
@MihaiGalos Edited the answer.
– Mirko
Mar 8 at 18:05
@MihaiGalos Edited the answer.
– Mirko
Mar 8 at 18:05
1
1
So the remaining items need to be default-constructed, which was producing the error. Of course. All elements are statically constructed. Awesome. Please remove the first answer and leave the one after the edit, perhaps add a few comments why this is needed. Then we can close this question! Many thanks!
– Mihai Galos
Mar 8 at 18:13
So the remaining items need to be default-constructed, which was producing the error. Of course. All elements are statically constructed. Awesome. Please remove the first answer and leave the one after the edit, perhaps add a few comments why this is needed. Then we can close this question! Many thanks!
– Mihai Galos
Mar 8 at 18:13
add a comment |
The error message could be better. But what's tripping the initialization is in fact that you don't have enough braces. Recall that a std::array
is an aggregate wrapping a raw array. So you need to initialize like this:
constexpr std::array<State, 10> states = OnEvent1, OnEvent2 ;
Otherwise, the somewhat inaccurate brace ellision detection algorithm assumes OnEvent1
is to initialize the internal array, and the second clause is redundant.
Now you just need to provide a default c'tor for State
, or adjust the array size.
thank you! I did not know I needed a default constructor for the the non-default initialized elements.
– Mihai Galos
Mar 8 at 18:22
1
@Mihai - Each c'tor is a different way to initialize objects of your class. The first two don't need it. It's the rest. You can also provide it in another way.TVoidVoid function = nullptr
will make your existing c'tor usable as a default c'tor too.
– StoryTeller
Mar 8 at 18:26
1
I'm a bit amazed that with extra (StoryTeller answer) and with less works too (my answer), but with the original quantity it did not.
– Mirko
Mar 8 at 18:26
1
@Mirko - Initialization in C++ is crazy, sadly.
– StoryTeller
Mar 8 at 18:27
add a comment |
The error message could be better. But what's tripping the initialization is in fact that you don't have enough braces. Recall that a std::array
is an aggregate wrapping a raw array. So you need to initialize like this:
constexpr std::array<State, 10> states = OnEvent1, OnEvent2 ;
Otherwise, the somewhat inaccurate brace ellision detection algorithm assumes OnEvent1
is to initialize the internal array, and the second clause is redundant.
Now you just need to provide a default c'tor for State
, or adjust the array size.
thank you! I did not know I needed a default constructor for the the non-default initialized elements.
– Mihai Galos
Mar 8 at 18:22
1
@Mihai - Each c'tor is a different way to initialize objects of your class. The first two don't need it. It's the rest. You can also provide it in another way.TVoidVoid function = nullptr
will make your existing c'tor usable as a default c'tor too.
– StoryTeller
Mar 8 at 18:26
1
I'm a bit amazed that with extra (StoryTeller answer) and with less works too (my answer), but with the original quantity it did not.
– Mirko
Mar 8 at 18:26
1
@Mirko - Initialization in C++ is crazy, sadly.
– StoryTeller
Mar 8 at 18:27
add a comment |
The error message could be better. But what's tripping the initialization is in fact that you don't have enough braces. Recall that a std::array
is an aggregate wrapping a raw array. So you need to initialize like this:
constexpr std::array<State, 10> states = OnEvent1, OnEvent2 ;
Otherwise, the somewhat inaccurate brace ellision detection algorithm assumes OnEvent1
is to initialize the internal array, and the second clause is redundant.
Now you just need to provide a default c'tor for State
, or adjust the array size.
The error message could be better. But what's tripping the initialization is in fact that you don't have enough braces. Recall that a std::array
is an aggregate wrapping a raw array. So you need to initialize like this:
constexpr std::array<State, 10> states = OnEvent1, OnEvent2 ;
Otherwise, the somewhat inaccurate brace ellision detection algorithm assumes OnEvent1
is to initialize the internal array, and the second clause is redundant.
Now you just need to provide a default c'tor for State
, or adjust the array size.
answered Mar 8 at 18:13
StoryTellerStoryTeller
106k13222285
106k13222285
thank you! I did not know I needed a default constructor for the the non-default initialized elements.
– Mihai Galos
Mar 8 at 18:22
1
@Mihai - Each c'tor is a different way to initialize objects of your class. The first two don't need it. It's the rest. You can also provide it in another way.TVoidVoid function = nullptr
will make your existing c'tor usable as a default c'tor too.
– StoryTeller
Mar 8 at 18:26
1
I'm a bit amazed that with extra (StoryTeller answer) and with less works too (my answer), but with the original quantity it did not.
– Mirko
Mar 8 at 18:26
1
@Mirko - Initialization in C++ is crazy, sadly.
– StoryTeller
Mar 8 at 18:27
add a comment |
thank you! I did not know I needed a default constructor for the the non-default initialized elements.
– Mihai Galos
Mar 8 at 18:22
1
@Mihai - Each c'tor is a different way to initialize objects of your class. The first two don't need it. It's the rest. You can also provide it in another way.TVoidVoid function = nullptr
will make your existing c'tor usable as a default c'tor too.
– StoryTeller
Mar 8 at 18:26
1
I'm a bit amazed that with extra (StoryTeller answer) and with less works too (my answer), but with the original quantity it did not.
– Mirko
Mar 8 at 18:26
1
@Mirko - Initialization in C++ is crazy, sadly.
– StoryTeller
Mar 8 at 18:27
thank you! I did not know I needed a default constructor for the the non-default initialized elements.
– Mihai Galos
Mar 8 at 18:22
thank you! I did not know I needed a default constructor for the the non-default initialized elements.
– Mihai Galos
Mar 8 at 18:22
1
1
@Mihai - Each c'tor is a different way to initialize objects of your class. The first two don't need it. It's the rest. You can also provide it in another way.
TVoidVoid function = nullptr
will make your existing c'tor usable as a default c'tor too.– StoryTeller
Mar 8 at 18:26
@Mihai - Each c'tor is a different way to initialize objects of your class. The first two don't need it. It's the rest. You can also provide it in another way.
TVoidVoid function = nullptr
will make your existing c'tor usable as a default c'tor too.– StoryTeller
Mar 8 at 18:26
1
1
I'm a bit amazed that with extra (StoryTeller answer) and with less works too (my answer), but with the original quantity it did not.
– Mirko
Mar 8 at 18:26
I'm a bit amazed that with extra (StoryTeller answer) and with less works too (my answer), but with the original quantity it did not.
– Mirko
Mar 8 at 18:26
1
1
@Mirko - Initialization in C++ is crazy, sadly.
– StoryTeller
Mar 8 at 18:27
@Mirko - Initialization in C++ is crazy, sadly.
– StoryTeller
Mar 8 at 18:27
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%2f55068356%2fstatically-initializing-constexpr-stdarray-of-objects-containing-function-poin%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