mardi 26 février 2019

constexpr const char* in header file

Is there a reason to not use "constexpr const char*" in header file?

The argument from a colleague is that every translation unit including this header file would have a copy.

My understanding was that since its compile-time constant, not memory is allocated and acts more like a "#define" macro with respect to memory usage. Here is the source,

TestConstExpr.h

  namespace TestConstExpr
  {
    constexpr const char* TIME_FORMAT = "yyyy-MM-dd hh:mm:ss";
    constexpr const int TIME_FORMAT_SIZE = sizeof(TIME_FORMAT) + 1;

    class TestClass
    {
        char arr[TIME_FORMAT_SIZE];
    }
  }

Aucun commentaire:

Enregistrer un commentaire