Explain constexpr with const const char * const

I have the following code:

static constexpr const char*const myString = "myString";

Could you explain what the difference is:

static const char*const myString = "myString";

What's new with constexpr in this case?

+4
source share
1 answer

The difference is described in the following quote from the C ++ Standard (9.4.2 Static Data Members)

3 const , -, -, (5.19). literal constexpr; , brace-or-equal-initializer, , - . [: , . -end note ] , odr-used (3.2) .

, ,

struct A
{
    const static double x = 1.0;
};

int main() 
{
    return 0;
}

struct A
{
    constexpr static double x = 1.0;
};

int main() 
{
    return 0;
}

, .

struct A
{
    static constexpr const char * myString = "myString";
};

int main() 
{
    return 0;
}

,

struct A
{
    static const char * const myString = "myString";
};

int main() 
{
    return 0;
}

.

+5

Source: https://habr.com/ru/post/1584442/


All Articles