Warning: ISO C++ forbids converting a string constant to ‘char*’ for a static `constexpr char*` data...












28
















This question already has an answer here:




  • constexpr const vs constexpr variables? [duplicate]

    3 answers




Why does this code return a warning




warning: ISO C++ forbids converting a string constant to ‘char*’
[-Wwrite-strings]




if




A constexpr specifier used in an object declaration or non-static
member function (until C++14) implies const. A constexpr specifier used in a function or static member variable (since C++17) declaration implies inline.




(cppreference.com)



#include <cassert>    
#include <string>
#include <iostream>

struct A
{
// warning: ISO C++ forbids converting a string constant to ‘char*’
static constexpr char* name_ = "A";
static constexpr char* name() { return name_; };
};

int main()
{};


If I add a const after constexpr, the warning is gone:



#include <cassert>    
#include <string>
#include <iostream>



struct A
{
static constexpr const char* name_ = "A";
static constexpr const char* name() { return name_; };
};

int main()
{};


With g++ --version = g++ (GCC) 8.2.1 20181127,



compilation g++ -O3 -std=c++2a -Wall main.cpp -o main.



Does the constexpr not imply const on static data members?










share|improve this question















marked as duplicate by Community Jan 19 at 11:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • stackoverflow.com/questions/28845058/…

    – Mat
    Jan 18 at 16:50











  • stackoverflow.com/questions/14116003/…

    – Brandon Lyons
    Jan 18 at 16:50
















28
















This question already has an answer here:




  • constexpr const vs constexpr variables? [duplicate]

    3 answers




Why does this code return a warning




warning: ISO C++ forbids converting a string constant to ‘char*’
[-Wwrite-strings]




if




A constexpr specifier used in an object declaration or non-static
member function (until C++14) implies const. A constexpr specifier used in a function or static member variable (since C++17) declaration implies inline.




(cppreference.com)



#include <cassert>    
#include <string>
#include <iostream>

struct A
{
// warning: ISO C++ forbids converting a string constant to ‘char*’
static constexpr char* name_ = "A";
static constexpr char* name() { return name_; };
};

int main()
{};


If I add a const after constexpr, the warning is gone:



#include <cassert>    
#include <string>
#include <iostream>



struct A
{
static constexpr const char* name_ = "A";
static constexpr const char* name() { return name_; };
};

int main()
{};


With g++ --version = g++ (GCC) 8.2.1 20181127,



compilation g++ -O3 -std=c++2a -Wall main.cpp -o main.



Does the constexpr not imply const on static data members?










share|improve this question















marked as duplicate by Community Jan 19 at 11:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • stackoverflow.com/questions/28845058/…

    – Mat
    Jan 18 at 16:50











  • stackoverflow.com/questions/14116003/…

    – Brandon Lyons
    Jan 18 at 16:50














28












28








28


2







This question already has an answer here:




  • constexpr const vs constexpr variables? [duplicate]

    3 answers




Why does this code return a warning




warning: ISO C++ forbids converting a string constant to ‘char*’
[-Wwrite-strings]




if




A constexpr specifier used in an object declaration or non-static
member function (until C++14) implies const. A constexpr specifier used in a function or static member variable (since C++17) declaration implies inline.




(cppreference.com)



#include <cassert>    
#include <string>
#include <iostream>

struct A
{
// warning: ISO C++ forbids converting a string constant to ‘char*’
static constexpr char* name_ = "A";
static constexpr char* name() { return name_; };
};

int main()
{};


If I add a const after constexpr, the warning is gone:



#include <cassert>    
#include <string>
#include <iostream>



struct A
{
static constexpr const char* name_ = "A";
static constexpr const char* name() { return name_; };
};

int main()
{};


With g++ --version = g++ (GCC) 8.2.1 20181127,



compilation g++ -O3 -std=c++2a -Wall main.cpp -o main.



Does the constexpr not imply const on static data members?










share|improve this question

















This question already has an answer here:




  • constexpr const vs constexpr variables? [duplicate]

    3 answers




Why does this code return a warning




warning: ISO C++ forbids converting a string constant to ‘char*’
[-Wwrite-strings]




if




A constexpr specifier used in an object declaration or non-static
member function (until C++14) implies const. A constexpr specifier used in a function or static member variable (since C++17) declaration implies inline.




(cppreference.com)



#include <cassert>    
#include <string>
#include <iostream>

struct A
{
// warning: ISO C++ forbids converting a string constant to ‘char*’
static constexpr char* name_ = "A";
static constexpr char* name() { return name_; };
};

int main()
{};


If I add a const after constexpr, the warning is gone:



#include <cassert>    
#include <string>
#include <iostream>



struct A
{
static constexpr const char* name_ = "A";
static constexpr const char* name() { return name_; };
};

int main()
{};


With g++ --version = g++ (GCC) 8.2.1 20181127,



compilation g++ -O3 -std=c++2a -Wall main.cpp -o main.



Does the constexpr not imply const on static data members?





This question already has an answer here:




  • constexpr const vs constexpr variables? [duplicate]

    3 answers








c++ c++11 static constexpr






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 18 at 17:15







tmaric

















asked Jan 18 at 16:47









tmarictmaric

2,7382052




2,7382052




marked as duplicate by Community Jan 19 at 11:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Community Jan 19 at 11:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • stackoverflow.com/questions/28845058/…

    – Mat
    Jan 18 at 16:50











  • stackoverflow.com/questions/14116003/…

    – Brandon Lyons
    Jan 18 at 16:50



















  • stackoverflow.com/questions/28845058/…

    – Mat
    Jan 18 at 16:50











  • stackoverflow.com/questions/14116003/…

    – Brandon Lyons
    Jan 18 at 16:50

















stackoverflow.com/questions/28845058/…

– Mat
Jan 18 at 16:50





stackoverflow.com/questions/28845058/…

– Mat
Jan 18 at 16:50













stackoverflow.com/questions/14116003/…

– Brandon Lyons
Jan 18 at 16:50





stackoverflow.com/questions/14116003/…

– Brandon Lyons
Jan 18 at 16:50












2 Answers
2






active

oldest

votes


















43














constexpr does imply const, but in this case it applies const to the "wrong thing".



constexpr char*


is basically the same as



char * const


which is a constant pointer to a non-const char. This won't work because string literals have the type const char[N] so it would cast away the constness of the array elements.



constexpr const char*


on the other hand, is basically the same as



char const * const


which is a constant pointer to a constant char, which is what you want as it preserves the constness of the elements.






share|improve this answer

































    11














    There is a usual difference between a constant pointer and a pointer to constant. By making your constexpr char* you made a pointer itself a constexpr (and, of course, const), but it still attempts to point at non-const character - and this is wrong, as string literals are const. Solution:



    constexpr const char* ch = "StackOverflow!";


    Which declares a constexpr pointer to const.






    share|improve this answer



















    • 1





      I remember the rule const binds to the thing to its immediate left (unless its first, in which case it binds to the thing to its immediate right). In my code, I tend to favor the general case, rather than the except-to-the-general rule in the parentheses. constexpr, when I use it (not frequently), I always put on the left as the first thing. I've not been bitten by the OP's situation yet.

      – Eljay
      Jan 18 at 17:03


















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    43














    constexpr does imply const, but in this case it applies const to the "wrong thing".



    constexpr char*


    is basically the same as



    char * const


    which is a constant pointer to a non-const char. This won't work because string literals have the type const char[N] so it would cast away the constness of the array elements.



    constexpr const char*


    on the other hand, is basically the same as



    char const * const


    which is a constant pointer to a constant char, which is what you want as it preserves the constness of the elements.






    share|improve this answer






























      43














      constexpr does imply const, but in this case it applies const to the "wrong thing".



      constexpr char*


      is basically the same as



      char * const


      which is a constant pointer to a non-const char. This won't work because string literals have the type const char[N] so it would cast away the constness of the array elements.



      constexpr const char*


      on the other hand, is basically the same as



      char const * const


      which is a constant pointer to a constant char, which is what you want as it preserves the constness of the elements.






      share|improve this answer




























        43












        43








        43







        constexpr does imply const, but in this case it applies const to the "wrong thing".



        constexpr char*


        is basically the same as



        char * const


        which is a constant pointer to a non-const char. This won't work because string literals have the type const char[N] so it would cast away the constness of the array elements.



        constexpr const char*


        on the other hand, is basically the same as



        char const * const


        which is a constant pointer to a constant char, which is what you want as it preserves the constness of the elements.






        share|improve this answer















        constexpr does imply const, but in this case it applies const to the "wrong thing".



        constexpr char*


        is basically the same as



        char * const


        which is a constant pointer to a non-const char. This won't work because string literals have the type const char[N] so it would cast away the constness of the array elements.



        constexpr const char*


        on the other hand, is basically the same as



        char const * const


        which is a constant pointer to a constant char, which is what you want as it preserves the constness of the elements.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 18 at 17:46









        Remy Lebeau

        336k18259453




        336k18259453










        answered Jan 18 at 16:52









        NathanOliverNathanOliver

        92.4k16129195




        92.4k16129195

























            11














            There is a usual difference between a constant pointer and a pointer to constant. By making your constexpr char* you made a pointer itself a constexpr (and, of course, const), but it still attempts to point at non-const character - and this is wrong, as string literals are const. Solution:



            constexpr const char* ch = "StackOverflow!";


            Which declares a constexpr pointer to const.






            share|improve this answer



















            • 1





              I remember the rule const binds to the thing to its immediate left (unless its first, in which case it binds to the thing to its immediate right). In my code, I tend to favor the general case, rather than the except-to-the-general rule in the parentheses. constexpr, when I use it (not frequently), I always put on the left as the first thing. I've not been bitten by the OP's situation yet.

              – Eljay
              Jan 18 at 17:03
















            11














            There is a usual difference between a constant pointer and a pointer to constant. By making your constexpr char* you made a pointer itself a constexpr (and, of course, const), but it still attempts to point at non-const character - and this is wrong, as string literals are const. Solution:



            constexpr const char* ch = "StackOverflow!";


            Which declares a constexpr pointer to const.






            share|improve this answer



















            • 1





              I remember the rule const binds to the thing to its immediate left (unless its first, in which case it binds to the thing to its immediate right). In my code, I tend to favor the general case, rather than the except-to-the-general rule in the parentheses. constexpr, when I use it (not frequently), I always put on the left as the first thing. I've not been bitten by the OP's situation yet.

              – Eljay
              Jan 18 at 17:03














            11












            11








            11







            There is a usual difference between a constant pointer and a pointer to constant. By making your constexpr char* you made a pointer itself a constexpr (and, of course, const), but it still attempts to point at non-const character - and this is wrong, as string literals are const. Solution:



            constexpr const char* ch = "StackOverflow!";


            Which declares a constexpr pointer to const.






            share|improve this answer













            There is a usual difference between a constant pointer and a pointer to constant. By making your constexpr char* you made a pointer itself a constexpr (and, of course, const), but it still attempts to point at non-const character - and this is wrong, as string literals are const. Solution:



            constexpr const char* ch = "StackOverflow!";


            Which declares a constexpr pointer to const.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 18 at 16:50









            SergeyASergeyA

            43k53886




            43k53886








            • 1





              I remember the rule const binds to the thing to its immediate left (unless its first, in which case it binds to the thing to its immediate right). In my code, I tend to favor the general case, rather than the except-to-the-general rule in the parentheses. constexpr, when I use it (not frequently), I always put on the left as the first thing. I've not been bitten by the OP's situation yet.

              – Eljay
              Jan 18 at 17:03














            • 1





              I remember the rule const binds to the thing to its immediate left (unless its first, in which case it binds to the thing to its immediate right). In my code, I tend to favor the general case, rather than the except-to-the-general rule in the parentheses. constexpr, when I use it (not frequently), I always put on the left as the first thing. I've not been bitten by the OP's situation yet.

              – Eljay
              Jan 18 at 17:03








            1




            1





            I remember the rule const binds to the thing to its immediate left (unless its first, in which case it binds to the thing to its immediate right). In my code, I tend to favor the general case, rather than the except-to-the-general rule in the parentheses. constexpr, when I use it (not frequently), I always put on the left as the first thing. I've not been bitten by the OP's situation yet.

            – Eljay
            Jan 18 at 17:03





            I remember the rule const binds to the thing to its immediate left (unless its first, in which case it binds to the thing to its immediate right). In my code, I tend to favor the general case, rather than the except-to-the-general rule in the parentheses. constexpr, when I use it (not frequently), I always put on the left as the first thing. I've not been bitten by the OP's situation yet.

            – Eljay
            Jan 18 at 17:03



            Popular posts from this blog

            Mario Kart Wii

            What does “Dominus providebit” mean?

            Antonio Litta Visconti Arese