mardi 28 mars 2023

What exactly is the difference between static constexpr const char * vs static const char * const in member variables

My question is are these two definitions below the same?


Class A 
{
    public: 
        static constexpr const char *something = "Something";
} 

And

Class A 
{
    public: 
        const char * const something = "Something";
} 

I understand that constexpr expressions are always evaluated at compile time and const expressions may or may not be. But in my particular case, it's just a string literal. Which means, the second expression

const char * const

also should be evaluated at compile time as per my understanding. Is my understanding right? Or, does the constexpr expression has any advantage over the const one?

Also, I'm aware that I can't declare a non-static member as constexpr because it won't be able to initialize until the object is instantiated. I wonder if this static adds any advantage to the constexpr expression in terms of efficiency?

Basically, I want to declare some constant strings and I want to do it in the most efficient way as this is for a firmware with limited memory and there are probably some 1000's of constant string literals.

Aucun commentaire:

Enregistrer un commentaire