Initialize an std::array algorithmically at compile timeInitialize array whose size is a compile-time constant to single valueStatic constant string (class member)Declaring an array whose size is declared as extern constIs the static initialization of global variables completed before `main()`?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsInitializing double at compile-timeCompile time detection of functionsIs there a way to enforce full initialization of std::arrayInitialize a static member of a class that includes an array?Why is this direct initialization valid? (C++ 17)Is it possible to automatically define a variable static or non-static depending on whether the initialization is constexpr?
Manager wants to hire me; HR does not. How to proceed?
Is there a term for someone whose preferred policies are a mix of Left and Right?
VHDL: What is correct way to model open collector output for FPGA?
Print the phrase "And she said, 'But that's his.'" using only the alphabet
Leveraging cash for buying car
How can Caller ID be faked?
SQL Server has encountered occurences of I/O requests taking longer than 15 seconds
Word-Ladder solver in Python 3
Leaving job close to major deadlines
2 Managed Packages in 1 Dev Org
At what temperature should the earth be cooked to prevent human infection?
How to know whether to write accidentals as sharps or flats?
How can I extract lat/long from GDB file in FME?
How does one transform a sum into a telescoping sum?
How useful is the GRE Exam?
How do you say you know OF something?
Why is gun control associated with the socially liberal Democratic party?
Can you cover a cube with copies of this shape?
I have found ports on my Samsung smart tv running a display service. What can I do with it?
Usage of "Es tut mir leid" by male German speakers and appropriate expressions to express empathy and understaning
What made the Ancient One do this in Endgame?
Testing thermite for chemical properties
Cut power on a remote Raspberry Pi 3 via another raspi
What uses does D&D 5e have for a d100?
Initialize an std::array algorithmically at compile time
Initialize array whose size is a compile-time constant to single valueStatic constant string (class member)Declaring an array whose size is declared as extern constIs the static initialization of global variables completed before `main()`?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsInitializing double at compile-timeCompile time detection of functionsIs there a way to enforce full initialization of std::arrayInitialize a static member of a class that includes an array?Why is this direct initialization valid? (C++ 17)Is it possible to automatically define a variable static or non-static depending on whether the initialization is constexpr?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Consider:
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 at compile time?
This is the final class in its entirety:
// https://www.nist.gov/pml/atomic-spectroscopy-compendium-basic-ideas-notation-data-and-formulas/atomic-spectroscopy
// https://www.nist.gov/pml/atomic-spectra-database
struct Spectrum
static constexpr unsigned _num_points 7810 ;
using Axis = std::array< double, _num_points >;
static constexpr Axis _x [] () // wavelength, nm
Axis a ;
for( unsigned i = 0; i < _num_points; ++i )
a[ i ] = 180 + 0.1 * i;
return a;
() ;
Axis _y ; // radiance, W·sr−1·m−2
;
The mixing of code and variables is unsightly, but at least the formula is right in front of the reader's eyes. Any other solution involved a lot of typing in order to get the in-class defined constant and type.
Or if I change my hearth, I can simply return the lambda at runtime.
c++ initialization c++17 compile-time stdarray
add a comment |
Consider:
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 at compile time?
This is the final class in its entirety:
// https://www.nist.gov/pml/atomic-spectroscopy-compendium-basic-ideas-notation-data-and-formulas/atomic-spectroscopy
// https://www.nist.gov/pml/atomic-spectra-database
struct Spectrum
static constexpr unsigned _num_points 7810 ;
using Axis = std::array< double, _num_points >;
static constexpr Axis _x [] () // wavelength, nm
Axis a ;
for( unsigned i = 0; i < _num_points; ++i )
a[ i ] = 180 + 0.1 * i;
return a;
() ;
Axis _y ; // radiance, W·sr−1·m−2
;
The mixing of code and variables is unsightly, but at least the formula is right in front of the reader's eyes. Any other solution involved a lot of typing in order to get the in-class defined constant and type.
Or if I change my hearth, I can simply return the lambda at runtime.
c++ initialization c++17 compile-time stdarray
2
Yes, see stackoverflow.com/a/56376301/2466431
– JVApen
May 30 at 18:34
1
If your data is really read-only with this pattern, for most use cases on most hardware you're better off computing it at runtime. 7810 * 8 bytes is a big cache footprint for the array. Loading a base + scale factor is only 2 doubles = 16 bytes. Computing at runtime is cheap: one int->FP conversion, and one FMA or a mul + add. (Plus loading the constants). So yes on a cache hit the LUT is faster, but especially for repeated use inside a loop just computing is often going to be good.
– Peter Cordes
Jun 1 at 11:22
add a comment |
Consider:
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 at compile time?
This is the final class in its entirety:
// https://www.nist.gov/pml/atomic-spectroscopy-compendium-basic-ideas-notation-data-and-formulas/atomic-spectroscopy
// https://www.nist.gov/pml/atomic-spectra-database
struct Spectrum
static constexpr unsigned _num_points 7810 ;
using Axis = std::array< double, _num_points >;
static constexpr Axis _x [] () // wavelength, nm
Axis a ;
for( unsigned i = 0; i < _num_points; ++i )
a[ i ] = 180 + 0.1 * i;
return a;
() ;
Axis _y ; // radiance, W·sr−1·m−2
;
The mixing of code and variables is unsightly, but at least the formula is right in front of the reader's eyes. Any other solution involved a lot of typing in order to get the in-class defined constant and type.
Or if I change my hearth, I can simply return the lambda at runtime.
c++ initialization c++17 compile-time stdarray
Consider:
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 at compile time?
This is the final class in its entirety:
// https://www.nist.gov/pml/atomic-spectroscopy-compendium-basic-ideas-notation-data-and-formulas/atomic-spectroscopy
// https://www.nist.gov/pml/atomic-spectra-database
struct Spectrum
static constexpr unsigned _num_points 7810 ;
using Axis = std::array< double, _num_points >;
static constexpr Axis _x [] () // wavelength, nm
Axis a ;
for( unsigned i = 0; i < _num_points; ++i )
a[ i ] = 180 + 0.1 * i;
return a;
() ;
Axis _y ; // radiance, W·sr−1·m−2
;
The mixing of code and variables is unsightly, but at least the formula is right in front of the reader's eyes. Any other solution involved a lot of typing in order to get the in-class defined constant and type.
Or if I change my hearth, I can simply return the lambda at runtime.
c++ initialization c++17 compile-time stdarray
c++ initialization c++17 compile-time stdarray
edited Jun 1 at 12:19
Vorac
asked May 30 at 18:06
VoracVorac
3,42853673
3,42853673
2
Yes, see stackoverflow.com/a/56376301/2466431
– JVApen
May 30 at 18:34
1
If your data is really read-only with this pattern, for most use cases on most hardware you're better off computing it at runtime. 7810 * 8 bytes is a big cache footprint for the array. Loading a base + scale factor is only 2 doubles = 16 bytes. Computing at runtime is cheap: one int->FP conversion, and one FMA or a mul + add. (Plus loading the constants). So yes on a cache hit the LUT is faster, but especially for repeated use inside a loop just computing is often going to be good.
– Peter Cordes
Jun 1 at 11:22
add a comment |
2
Yes, see stackoverflow.com/a/56376301/2466431
– JVApen
May 30 at 18:34
1
If your data is really read-only with this pattern, for most use cases on most hardware you're better off computing it at runtime. 7810 * 8 bytes is a big cache footprint for the array. Loading a base + scale factor is only 2 doubles = 16 bytes. Computing at runtime is cheap: one int->FP conversion, and one FMA or a mul + add. (Plus loading the constants). So yes on a cache hit the LUT is faster, but especially for repeated use inside a loop just computing is often going to be good.
– Peter Cordes
Jun 1 at 11:22
2
2
Yes, see stackoverflow.com/a/56376301/2466431
– JVApen
May 30 at 18:34
Yes, see stackoverflow.com/a/56376301/2466431
– JVApen
May 30 at 18:34
1
1
If your data is really read-only with this pattern, for most use cases on most hardware you're better off computing it at runtime. 7810 * 8 bytes is a big cache footprint for the array. Loading a base + scale factor is only 2 doubles = 16 bytes. Computing at runtime is cheap: one int->FP conversion, and one FMA or a mul + add. (Plus loading the constants). So yes on a cache hit the LUT is faster, but especially for repeated use inside a loop just computing is often going to be good.
– Peter Cordes
Jun 1 at 11:22
If your data is really read-only with this pattern, for most use cases on most hardware you're better off computing it at runtime. 7810 * 8 bytes is a big cache footprint for the array. Loading a base + scale factor is only 2 doubles = 16 bytes. Computing at runtime is cheap: one int->FP conversion, and one FMA or a mul + add. (Plus loading the constants). So yes on a cache hit the LUT is faster, but especially for repeated use inside a loop just computing is often going to be good.
– Peter Cordes
Jun 1 at 11:22
add a comment |
3 Answers
3
active
oldest
votes
For completeness' sake, here's a version that does not require the definition of a function but instead uses a lambda. C++17 introduced the ability of using lambdas in constant expressions, so you can declare your array constexpr and use a lambda to initialize it:
static constexpr auto axis = []
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 = []
auto a = decltype(axis);
for (int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
();
1
You don't need theconstexprin the lambda :)
– Rakete1111
May 31 at 9:03
1
@Rakete1111 You are right. The lambda's call operator is markedconstexprautomatically.
– Nikos C.
May 31 at 14:32
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>();
;
3
Suggest substitutingstd::array<double,num_points>forautoso the reader doesn't have to check the initializing function to know the type.
– doug
May 30 at 18:59
4
@doug I find it to be the matter of preference. Some people like to know the type, some people prefer lesstypeing (pun natural!).
– SergeyA
May 30 at 19:02
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
May 30 at 19:06
1
@doug your code editor doesn't give you the type and doesn't allow you to jump to function in a single click?
– SergeyA
May 30 at 19:08
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
May 30 at 19:09
add a comment |
There's also the std::index_sequence trick (Wandbox example):
template <unsigned... i>
static constexpr auto init_axis(std::integer_sequence<unsigned, i...>)
return std::array(180 + 0.1 * i)...;
;
static constexpr auto axis = init_axis(std::make_integer_sequence<unsigned, num_points>);
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-stdarray-algorithmically-at-compile-time%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
For completeness' sake, here's a version that does not require the definition of a function but instead uses a lambda. C++17 introduced the ability of using lambdas in constant expressions, so you can declare your array constexpr and use a lambda to initialize it:
static constexpr auto axis = []
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 = []
auto a = decltype(axis);
for (int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
();
1
You don't need theconstexprin the lambda :)
– Rakete1111
May 31 at 9:03
1
@Rakete1111 You are right. The lambda's call operator is markedconstexprautomatically.
– Nikos C.
May 31 at 14:32
add a comment |
For completeness' sake, here's a version that does not require the definition of a function but instead uses a lambda. C++17 introduced the ability of using lambdas in constant expressions, so you can declare your array constexpr and use a lambda to initialize it:
static constexpr auto axis = []
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 = []
auto a = decltype(axis);
for (int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
();
1
You don't need theconstexprin the lambda :)
– Rakete1111
May 31 at 9:03
1
@Rakete1111 You are right. The lambda's call operator is markedconstexprautomatically.
– Nikos C.
May 31 at 14:32
add a comment |
For completeness' sake, here's a version that does not require the definition of a function but instead uses a lambda. C++17 introduced the ability of using lambdas in constant expressions, so you can declare your array constexpr and use a lambda to initialize it:
static constexpr auto axis = []
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 = []
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 lambda. C++17 introduced the ability of using lambdas in constant expressions, so you can declare your array constexpr and use a lambda to initialize it:
static constexpr auto axis = []
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 = []
auto a = decltype(axis);
for (int i = 0; i < num_points; ++i)
a[i] = 180 + 0.1 * i;
return a;
();
edited May 31 at 23:25
answered May 30 at 18:39
Nikos C.Nikos C.
37.8k54170
37.8k54170
1
You don't need theconstexprin the lambda :)
– Rakete1111
May 31 at 9:03
1
@Rakete1111 You are right. The lambda's call operator is markedconstexprautomatically.
– Nikos C.
May 31 at 14:32
add a comment |
1
You don't need theconstexprin the lambda :)
– Rakete1111
May 31 at 9:03
1
@Rakete1111 You are right. The lambda's call operator is markedconstexprautomatically.
– Nikos C.
May 31 at 14:32
1
1
You don't need the
constexpr in the lambda :)– Rakete1111
May 31 at 9:03
You don't need the
constexpr in the lambda :)– Rakete1111
May 31 at 9:03
1
1
@Rakete1111 You are right. The lambda's call operator is marked
constexpr automatically.– Nikos C.
May 31 at 14:32
@Rakete1111 You are right. The lambda's call operator is marked
constexpr automatically.– Nikos C.
May 31 at 14:32
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>();
;
3
Suggest substitutingstd::array<double,num_points>forautoso the reader doesn't have to check the initializing function to know the type.
– doug
May 30 at 18:59
4
@doug I find it to be the matter of preference. Some people like to know the type, some people prefer lesstypeing (pun natural!).
– SergeyA
May 30 at 19:02
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
May 30 at 19:06
1
@doug your code editor doesn't give you the type and doesn't allow you to jump to function in a single click?
– SergeyA
May 30 at 19:08
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
May 30 at 19:09
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>();
;
3
Suggest substitutingstd::array<double,num_points>forautoso the reader doesn't have to check the initializing function to know the type.
– doug
May 30 at 18:59
4
@doug I find it to be the matter of preference. Some people like to know the type, some people prefer lesstypeing (pun natural!).
– SergeyA
May 30 at 19:02
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
May 30 at 19:06
1
@doug your code editor doesn't give you the type and doesn't allow you to jump to function in a single click?
– SergeyA
May 30 at 19:08
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
May 30 at 19:09
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 May 30 at 18:49
answered May 30 at 18:08
SergeyASergeyA
46.7k54395
46.7k54395
3
Suggest substitutingstd::array<double,num_points>forautoso the reader doesn't have to check the initializing function to know the type.
– doug
May 30 at 18:59
4
@doug I find it to be the matter of preference. Some people like to know the type, some people prefer lesstypeing (pun natural!).
– SergeyA
May 30 at 19:02
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
May 30 at 19:06
1
@doug your code editor doesn't give you the type and doesn't allow you to jump to function in a single click?
– SergeyA
May 30 at 19:08
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
May 30 at 19:09
add a comment |
3
Suggest substitutingstd::array<double,num_points>forautoso the reader doesn't have to check the initializing function to know the type.
– doug
May 30 at 18:59
4
@doug I find it to be the matter of preference. Some people like to know the type, some people prefer lesstypeing (pun natural!).
– SergeyA
May 30 at 19:02
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
May 30 at 19:06
1
@doug your code editor doesn't give you the type and doesn't allow you to jump to function in a single click?
– SergeyA
May 30 at 19:08
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
May 30 at 19:09
3
3
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
May 30 at 18:59
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
May 30 at 18:59
4
4
@doug I find it to be the matter of preference. Some people like to know the type, some people prefer less
typeing (pun natural!).– SergeyA
May 30 at 19:02
@doug I find it to be the matter of preference. Some people like to know the type, some people prefer less
typeing (pun natural!).– SergeyA
May 30 at 19:02
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
May 30 at 19:06
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
May 30 at 19:06
1
1
@doug your code editor doesn't give you the type and doesn't allow you to jump to function in a single click?
– SergeyA
May 30 at 19:08
@doug your code editor doesn't give you the type and doesn't allow you to jump to function in a single click?
– SergeyA
May 30 at 19:08
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
May 30 at 19:09
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
May 30 at 19:09
add a comment |
There's also the std::index_sequence trick (Wandbox example):
template <unsigned... i>
static constexpr auto init_axis(std::integer_sequence<unsigned, i...>)
return std::array(180 + 0.1 * i)...;
;
static constexpr auto axis = init_axis(std::make_integer_sequence<unsigned, num_points>);
add a comment |
There's also the std::index_sequence trick (Wandbox example):
template <unsigned... i>
static constexpr auto init_axis(std::integer_sequence<unsigned, i...>)
return std::array(180 + 0.1 * i)...;
;
static constexpr auto axis = init_axis(std::make_integer_sequence<unsigned, num_points>);
add a comment |
There's also the std::index_sequence trick (Wandbox example):
template <unsigned... i>
static constexpr auto init_axis(std::integer_sequence<unsigned, i...>)
return std::array(180 + 0.1 * i)...;
;
static constexpr auto axis = init_axis(std::make_integer_sequence<unsigned, num_points>);
There's also the std::index_sequence trick (Wandbox example):
template <unsigned... i>
static constexpr auto init_axis(std::integer_sequence<unsigned, i...>)
return std::array(180 + 0.1 * i)...;
;
static constexpr auto axis = init_axis(std::make_integer_sequence<unsigned, num_points>);
edited May 31 at 8:29
answered May 31 at 6:38
metalfoxmetalfox
2,477624
2,477624
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-stdarray-algorithmically-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
2
Yes, see stackoverflow.com/a/56376301/2466431
– JVApen
May 30 at 18:34
1
If your data is really read-only with this pattern, for most use cases on most hardware you're better off computing it at runtime. 7810 * 8 bytes is a big cache footprint for the array. Loading a base + scale factor is only 2 doubles = 16 bytes. Computing at runtime is cheap: one int->FP conversion, and one FMA or a mul + add. (Plus loading the constants). So yes on a cache hit the LUT is faster, but especially for repeated use inside a loop just computing is often going to be good.
– Peter Cordes
Jun 1 at 11:22