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?

How much caffeine would there be if I reuse tea leaves in a second brewing?

Why do some switching regulator require tantalum or electrolytic capacitors instead of ceramic?

What is this second smaller runway next to London City Airport?

Can I take the high-speed bullet train Beijing–Hong Kong under Chinese 144 h visa-free transit rules?

Is the assumption of normality of the error term needed to use p-value?

network cable - why T-568A and B standard

Scrum Team and Product Owner working against each other

How does kinetic energy work in braking a vehicle?

I am particularly fascinated by the Chinese character that is pronounced SHIN & means faith or belief

What's the most profitable use for an elemental transmuter?

Create a program that prints the amount of characters it has, in words

What does “studies need to be taken with more than the usual grain of salt” mean?

What term can we propose for someone who prioritizes equipment over musicianship?

What techniques can I use to seduce a PC without arousing suspicion of ulterior motives?

How does AT-AT deploy troops?

why we need self-synchronization?

What happens to a Bladesinger reincarnated as a Human?

How wavy is an array?

Make a haystack (with a needle)

Why is more music written in sharp keys than flat keys?

For a command to increase something, should instructions refer to the "+" key or the "=" key?

Why would Climate activists disrupt public transport?

Why did the people of Zion never find evidence of the previous cycles?

How can an immortal member of the nobility be prevented from taking the throne?



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
margin-bottom:0;









35

















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.










share|improve this question























  • 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

















35

















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.










share|improve this question























  • 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













35












35








35


9






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.










share|improve this question

















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






share|improve this question
















share|improve this question













share|improve this question




share|improve this question








edited Jun 1 at 12:19







Vorac

















asked May 30 at 18:06









VoracVorac

3,5506 gold badges37 silver badges75 bronze badges




3,5506 gold badges37 silver badges75 bronze badges










  • 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





    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












3 Answers
3






active

oldest

votes


















35


















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;
();





share|improve this answer























  • 1





    You don't need the constexpr in the lambda :)

    – Rakete1111
    May 31 at 9:03






  • 1





    @Rakete1111 You are right. The lambda's call operator is marked constexpr automatically.

    – Nikos C.
    May 31 at 14:32


















31


















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>();
;





share|improve this answer























  • 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






  • 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











  • 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


















13


















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>);





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/4.0/"u003ecc by-sa 4.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%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









    35


















    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;
    ();





    share|improve this answer























    • 1





      You don't need the constexpr in the lambda :)

      – Rakete1111
      May 31 at 9:03






    • 1





      @Rakete1111 You are right. The lambda's call operator is marked constexpr automatically.

      – Nikos C.
      May 31 at 14:32















    35


















    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;
    ();





    share|improve this answer























    • 1





      You don't need the constexpr in the lambda :)

      – Rakete1111
      May 31 at 9:03






    • 1





      @Rakete1111 You are right. The lambda's call operator is marked constexpr automatically.

      – Nikos C.
      May 31 at 14:32













    35














    35










    35









    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;
    ();





    share|improve this answer
















    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;
    ();






    share|improve this answer















    share|improve this answer




    share|improve this answer








    edited May 31 at 23:25

























    answered May 30 at 18:39









    Nikos C.Nikos C.

    41.9k7 gold badges50 silver badges79 bronze badges




    41.9k7 gold badges50 silver badges79 bronze badges










    • 1





      You don't need the constexpr in the lambda :)

      – Rakete1111
      May 31 at 9:03






    • 1





      @Rakete1111 You are right. The lambda's call operator is marked constexpr automatically.

      – Nikos C.
      May 31 at 14:32












    • 1





      You don't need the constexpr in the lambda :)

      – Rakete1111
      May 31 at 9:03






    • 1





      @Rakete1111 You are right. The lambda's call operator is marked constexpr automatically.

      – 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













    31


















    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>();
    ;





    share|improve this answer























    • 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






    • 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











    • 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















    31


















    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>();
    ;





    share|improve this answer























    • 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






    • 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











    • 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













    31














    31










    31









    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>();
    ;





    share|improve this answer
















    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>();
    ;






    share|improve this answer















    share|improve this answer




    share|improve this answer








    edited May 30 at 18:49

























    answered May 30 at 18:08









    SergeyASergeyA

    49.9k5 gold badges49 silver badges103 bronze badges




    49.9k5 gold badges49 silver badges103 bronze badges










    • 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






    • 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











    • 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





      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





      @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







    • 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











    13


















    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>);





    share|improve this answer
































      13


















      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>);





      share|improve this answer






























        13














        13










        13









        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>);





        share|improve this answer
















        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>);






        share|improve this answer















        share|improve this answer




        share|improve this answer








        edited May 31 at 8:29

























        answered May 31 at 6:38









        metalfoxmetalfox

        2,8349 silver badges27 bronze badges




        2,8349 silver badges27 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%2f56383454%2finitialize-an-stdarray-algorithmically-at-compile-time%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

            Distance measures on a map of a game The 2019 Stack Overflow Developer Survey Results Are Inmin distance in a graphShortest distance path on contour plotHow to plot a tilted map?Finding points outside of a diskDelaunay link distanceAnnulus from GeoDisks: drawing a ring on a mapNegative Correlation DistanceFind distance along a path (GPS coordinates)Finding position at given distance in a GeoPathMathematics behind distance estimation using camera

            How to get a smooth, uniform ParametricPlot of a 2D Region?How to plot a complicated Region?How to exclude a region from ParametricPlotHow discretize a region placing vertices on a specific non-uniform gridHow to transform a Plot or a ParametricPlot into a RegionHow can I get a smooth plot of a bounded region?Smooth ParametricPlot3D with RegionFunction?Smooth border of a region ParametricPlotSmooth region boundarySmooth region plot from list of pointsGet minimum y of a certain x in a region

            Genealogie vun de Merowenger Vum Merowech bis zum Chilperich I. | Navigatiounsmenü