Initialize an array of doubles at compile timeInitialize array whose size is a compile-time constant to single valueRuntime vs Compile timeStatic constant string (class member)Initializing const member within class declaration in C++Why is it faster to process a sorted array than an unsorted array?C++11: Compile Time Calculation of ArrayInitializing a `static constexpr double` with MSVC 2013Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsInitializing double at compile-timeCompiling an application for use in highly radioactive environmentsHow to accomplish C++11 compile time class static member initialization on a bitset?
Opposite of "Squeaky wheel gets the grease"
Was the 1959 Tibetan Uprising really an uprising?
Is it possible for people to live in the eye of a permanent hypercane?
Rotated Position of Integers
PhD student with mental health issues and bad performance
Short story written from alien perspective with this line: "It's too bright to look at, so they don't"
Asking bank to reduce APR instead of increasing credit limit
Why were the Night's Watch required to be celibate?
Can commodity 3d printer extrusion hardware and filament be used for injection molding?
What is the best option to connect old computer to modern TV
Does any lore text explain why the planes of Acheron, Gehenna, and Carceri are the alignment they are?
Where do the UpdateInstallationWizard.aspx logs get saved in Azure PaaS?
Unorthodox way of solving Einstein field equations
Is there a rule that prohibits us from using 2 possessives in a row?
Will dual-learning in a glider make my airplane learning safer?
Did thousands of women die every year due to illegal abortions before Roe v. Wade?
Can a magnetic field of a large body be stronger than its gravity?
Is there any Biblical Basis for 400 years of silence between Old and New Testament?
Why is Colorado so different politically from nearby states?
Hygienic footwear for prehensile feet?
Is having a hidden directory under /etc safe?
Should we freeze the number of people coming in to the study for Kaplan-Meier test
Do I add my ability modifier to the damage of the bonus-action attack granted by the Crossbow Expert feat?
Is it possible to kill all life on Earth?
Initialize an array of doubles at compile time
Initialize array whose size is a compile-time constant to single valueRuntime vs Compile timeStatic constant string (class member)Initializing const member within class declaration in C++Why is it faster to process a sorted array than an unsorted array?C++11: Compile Time Calculation of ArrayInitializing a `static constexpr double` with MSVC 2013Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsInitializing double at compile-timeCompiling an application for use in highly radioactive environmentsHow to accomplish C++11 compile time class static member initialization on a bitset?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
static constexpr unsigned num_points 7810 ;
std::array< double, num_points > axis;
for(int i = 0; i < num_points; ++i)
axis[i] = 180 + 0.1 * i;
axis
is a class-wide constant. I want to avoid initializing it like any other global variable. Can it be done in compile time?
c++ c++17 compile-time
add a comment |
static constexpr unsigned num_points 7810 ;
std::array< double, num_points > axis;
for(int i = 0; i < num_points; ++i)
axis[i] = 180 + 0.1 * i;
axis
is a class-wide constant. I want to avoid initializing it like any other global variable. Can it be done in compile time?
c++ c++17 compile-time
Yes, see stackoverflow.com/a/56376301/2466431
– JVApen
8 hours ago
add a comment |
static constexpr unsigned num_points 7810 ;
std::array< double, num_points > axis;
for(int i = 0; i < num_points; ++i)
axis[i] = 180 + 0.1 * i;
axis
is a class-wide constant. I want to avoid initializing it like any other global variable. Can it be done in compile time?
c++ c++17 compile-time
static constexpr unsigned num_points 7810 ;
std::array< double, num_points > axis;
for(int i = 0; i < num_points; ++i)
axis[i] = 180 + 0.1 * i;
axis
is a class-wide constant. I want to avoid initializing it like any other global variable. Can it be done in compile time?
c++ c++17 compile-time
c++ c++17 compile-time
edited 8 hours ago
Barry
191k21347630
191k21347630
asked 8 hours ago
VoracVorac
3,30353572
3,30353572
Yes, see stackoverflow.com/a/56376301/2466431
– JVApen
8 hours ago
add a comment |
Yes, see stackoverflow.com/a/56376301/2466431
– JVApen
8 hours ago
Yes, see stackoverflow.com/a/56376301/2466431
– JVApen
8 hours ago
Yes, see stackoverflow.com/a/56376301/2466431
– JVApen
8 hours ago
add a comment |
2 Answers
2
active
oldest
votes
Here is the full compileable code:
#include <array>
template<int num_points>
static constexpr std::array<double, num_points> init_axis()
std::array<double, num_points> a;
for(int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
;
struct Z
static constexpr int num_points = 10;
static constexpr auto axis = init_axis<num_points>();
;
Suggest substitutingstd::array<double,num_points>
forauto
so the reader doesn't have to check the initializing function to know the type.
– doug
7 hours ago
@doug I find it to be the matter of preference. Some people like to know the type, some people prefer lesstype
ing (pun natural!).
– SergeyA
7 hours ago
I prefer auto too, for the same reason. But when the type isn't obvious for someone reading the line of code, including me a month later, I'll put in the extra typing. Probably helpful for readers coming to the question as well.
– doug
7 hours ago
@doug your code editor doesn't give you the type and doesn't allow you to jump to function in a single click?
– SergeyA
7 hours ago
Of course. But when just scanning a screen of code it doesn't. Have to put the cursor on the variable. I prefer to avoid that.
– doug
7 hours ago
add a comment |
For completeness' sake, here's a version that does not require the definition of a function but instead uses a constexpr
lambda:
static constexpr auto axis = []() constexpr
std::array<double, num_points> a;
for(int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
();
(Note the ()
in the last line, which calls the lambda right away.)
If you don't like the auto
in the axis
declaration because it makes it harder to read the actual type, but you don't want to repeat the type inside the lambda, you can instead do:
static constexpr std::array<double, num_points> axis = []() constexpr
auto a = decltype(axis);
for(int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
();
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%2f56383454%2finitialize-an-array-of-doubles-at-compile-time%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
Here is the full compileable code:
#include <array>
template<int num_points>
static constexpr std::array<double, num_points> init_axis()
std::array<double, num_points> a;
for(int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
;
struct Z
static constexpr int num_points = 10;
static constexpr auto axis = init_axis<num_points>();
;
Suggest substitutingstd::array<double,num_points>
forauto
so the reader doesn't have to check the initializing function to know the type.
– doug
7 hours ago
@doug I find it to be the matter of preference. Some people like to know the type, some people prefer lesstype
ing (pun natural!).
– SergeyA
7 hours ago
I prefer auto too, for the same reason. But when the type isn't obvious for someone reading the line of code, including me a month later, I'll put in the extra typing. Probably helpful for readers coming to the question as well.
– doug
7 hours ago
@doug your code editor doesn't give you the type and doesn't allow you to jump to function in a single click?
– SergeyA
7 hours ago
Of course. But when just scanning a screen of code it doesn't. Have to put the cursor on the variable. I prefer to avoid that.
– doug
7 hours ago
add a comment |
Here is the full compileable code:
#include <array>
template<int num_points>
static constexpr std::array<double, num_points> init_axis()
std::array<double, num_points> a;
for(int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
;
struct Z
static constexpr int num_points = 10;
static constexpr auto axis = init_axis<num_points>();
;
Suggest substitutingstd::array<double,num_points>
forauto
so the reader doesn't have to check the initializing function to know the type.
– doug
7 hours ago
@doug I find it to be the matter of preference. Some people like to know the type, some people prefer lesstype
ing (pun natural!).
– SergeyA
7 hours ago
I prefer auto too, for the same reason. But when the type isn't obvious for someone reading the line of code, including me a month later, I'll put in the extra typing. Probably helpful for readers coming to the question as well.
– doug
7 hours ago
@doug your code editor doesn't give you the type and doesn't allow you to jump to function in a single click?
– SergeyA
7 hours ago
Of course. But when just scanning a screen of code it doesn't. Have to put the cursor on the variable. I prefer to avoid that.
– doug
7 hours ago
add a comment |
Here is the full compileable code:
#include <array>
template<int num_points>
static constexpr std::array<double, num_points> init_axis()
std::array<double, num_points> a;
for(int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
;
struct Z
static constexpr int num_points = 10;
static constexpr auto axis = init_axis<num_points>();
;
Here is the full compileable code:
#include <array>
template<int num_points>
static constexpr std::array<double, num_points> init_axis()
std::array<double, num_points> a;
for(int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
;
struct Z
static constexpr int num_points = 10;
static constexpr auto axis = init_axis<num_points>();
;
edited 7 hours ago
answered 8 hours ago
SergeyASergeyA
46.2k54194
46.2k54194
Suggest substitutingstd::array<double,num_points>
forauto
so the reader doesn't have to check the initializing function to know the type.
– doug
7 hours ago
@doug I find it to be the matter of preference. Some people like to know the type, some people prefer lesstype
ing (pun natural!).
– SergeyA
7 hours ago
I prefer auto too, for the same reason. But when the type isn't obvious for someone reading the line of code, including me a month later, I'll put in the extra typing. Probably helpful for readers coming to the question as well.
– doug
7 hours ago
@doug your code editor doesn't give you the type and doesn't allow you to jump to function in a single click?
– SergeyA
7 hours ago
Of course. But when just scanning a screen of code it doesn't. Have to put the cursor on the variable. I prefer to avoid that.
– doug
7 hours ago
add a comment |
Suggest substitutingstd::array<double,num_points>
forauto
so the reader doesn't have to check the initializing function to know the type.
– doug
7 hours ago
@doug I find it to be the matter of preference. Some people like to know the type, some people prefer lesstype
ing (pun natural!).
– SergeyA
7 hours ago
I prefer auto too, for the same reason. But when the type isn't obvious for someone reading the line of code, including me a month later, I'll put in the extra typing. Probably helpful for readers coming to the question as well.
– doug
7 hours ago
@doug your code editor doesn't give you the type and doesn't allow you to jump to function in a single click?
– SergeyA
7 hours ago
Of course. But when just scanning a screen of code it doesn't. Have to put the cursor on the variable. I prefer to avoid that.
– doug
7 hours ago
Suggest substituting
std::array<double,num_points>
for auto
so the reader doesn't have to check the initializing function to know the type.– doug
7 hours ago
Suggest substituting
std::array<double,num_points>
for auto
so the reader doesn't have to check the initializing function to know the type.– doug
7 hours ago
@doug I find it to be the matter of preference. Some people like to know the type, some people prefer less
type
ing (pun natural!).– SergeyA
7 hours ago
@doug I find it to be the matter of preference. Some people like to know the type, some people prefer less
type
ing (pun natural!).– SergeyA
7 hours ago
I prefer auto too, for the same reason. But when the type isn't obvious for someone reading the line of code, including me a month later, I'll put in the extra typing. Probably helpful for readers coming to the question as well.
– doug
7 hours ago
I prefer auto too, for the same reason. But when the type isn't obvious for someone reading the line of code, including me a month later, I'll put in the extra typing. Probably helpful for readers coming to the question as well.
– doug
7 hours ago
@doug your code editor doesn't give you the type and doesn't allow you to jump to function in a single click?
– SergeyA
7 hours ago
@doug your code editor doesn't give you the type and doesn't allow you to jump to function in a single click?
– SergeyA
7 hours ago
Of course. But when just scanning a screen of code it doesn't. Have to put the cursor on the variable. I prefer to avoid that.
– doug
7 hours ago
Of course. But when just scanning a screen of code it doesn't. Have to put the cursor on the variable. I prefer to avoid that.
– doug
7 hours ago
add a comment |
For completeness' sake, here's a version that does not require the definition of a function but instead uses a constexpr
lambda:
static constexpr auto axis = []() constexpr
std::array<double, num_points> a;
for(int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
();
(Note the ()
in the last line, which calls the lambda right away.)
If you don't like the auto
in the axis
declaration because it makes it harder to read the actual type, but you don't want to repeat the type inside the lambda, you can instead do:
static constexpr std::array<double, num_points> axis = []() constexpr
auto a = decltype(axis);
for(int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
();
add a comment |
For completeness' sake, here's a version that does not require the definition of a function but instead uses a constexpr
lambda:
static constexpr auto axis = []() constexpr
std::array<double, num_points> a;
for(int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
();
(Note the ()
in the last line, which calls the lambda right away.)
If you don't like the auto
in the axis
declaration because it makes it harder to read the actual type, but you don't want to repeat the type inside the lambda, you can instead do:
static constexpr std::array<double, num_points> axis = []() constexpr
auto a = decltype(axis);
for(int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
();
add a comment |
For completeness' sake, here's a version that does not require the definition of a function but instead uses a constexpr
lambda:
static constexpr auto axis = []() constexpr
std::array<double, num_points> a;
for(int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
();
(Note the ()
in the last line, which calls the lambda right away.)
If you don't like the auto
in the axis
declaration because it makes it harder to read the actual type, but you don't want to repeat the type inside the lambda, you can instead do:
static constexpr std::array<double, num_points> axis = []() constexpr
auto a = decltype(axis);
for(int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
();
For completeness' sake, here's a version that does not require the definition of a function but instead uses a constexpr
lambda:
static constexpr auto axis = []() constexpr
std::array<double, num_points> a;
for(int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
();
(Note the ()
in the last line, which calls the lambda right away.)
If you don't like the auto
in the axis
declaration because it makes it harder to read the actual type, but you don't want to repeat the type inside the lambda, you can instead do:
static constexpr std::array<double, num_points> axis = []() constexpr
auto a = decltype(axis);
for(int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
();
edited 7 hours ago
answered 8 hours ago
Nikos C.Nikos C.
36.5k54069
36.5k54069
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%2f56383454%2finitialize-an-array-of-doubles-at-compile-time%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
Yes, see stackoverflow.com/a/56376301/2466431
– JVApen
8 hours ago