dimanche 20 janvier 2019

Can C++ functions return a pointer to an array of known length?

I have a class that contains a static constexpr array of const chars, which i would like to make available via a c_str() method:

class my_class {
  private:
    static constexpr const char c_str_[6] = {'c', 'h', 'a', 'r', 's', '\0'};
  public:
    static constexpr const char* c_str() {
      return c_str_;
    }
};

This works, but has an unfortunate effect: It removes the length of the pointed-to array from the type:

decltype(my_class::c_str()) // equivalent to const char*


What I'd really like is some way to achieve this:

decltype(my_class::c_str()) // equivalent to const char[6]

I understand that in either case the returned object will be a pointer; I would just like to preserve the length of the pointed-to array in the type. Kind of like how decltype("string literal") is const char[15], not const char*.

Is there a way to do this?

Aucun commentaire:

Enregistrer un commentaire