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;








11















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?










share|improve this question
























  • Yes, see stackoverflow.com/a/56376301/2466431

    – JVApen
    8 hours ago

















11















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?










share|improve this question
























  • Yes, see stackoverflow.com/a/56376301/2466431

    – JVApen
    8 hours ago













11












11








11


1






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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












2 Answers
2






active

oldest

votes


















13














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

























  • 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 typeing (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


















7














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





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



    );













    draft saved

    draft discarded


















    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









    13














    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

























    • 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 typeing (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















    13














    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

























    • 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 typeing (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













    13












    13








    13







    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 7 hours ago

























    answered 8 hours ago









    SergeyASergeyA

    46.2k54194




    46.2k54194












    • 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 typeing (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











    • @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
      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 typeing (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 typeing (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













    7














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





    share|improve this answer





























      7














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





      share|improve this answer



























        7












        7








        7







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





        share|improve this answer















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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 7 hours ago

























        answered 8 hours ago









        Nikos C.Nikos C.

        36.5k54069




        36.5k54069



























            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-array-of-doubles-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

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Ласкавець круглолистий Зміст Опис | Поширення | Галерея | Примітки | Посилання | Навігаційне меню58171138361-22960890446Bupleurum rotundifoliumEuro+Med PlantbasePlants of the World Online — Kew ScienceGermplasm Resources Information Network (GRIN)Ласкавецькн. VI : Літери Ком — Левиправивши або дописавши її