Q:

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?

A:

You mean like returning a reference to c_str_?
static constexpr decltype(c_str_)& c_str() { return c_str_; }

or
static constexpr auto& c_str() { return c_str_; }

If you want a pointer, just swap the & for a * and return &c_str_.
If you want to explicitly refer to the type, use an alias:
using T = const char[6];
static constexpr T& c_str() { return c_str_; }

Or if you really hate yourself:
static constexpr const char (&c_str())[6] { return c_str_; }

Note that you cannot have a function return a raw array by value.

A:

A modern alternative would be returning a string_view, which basically is the combination of a pointer to the string and the length. That allows the user of your function to directly access the length information. And the string could be stored in a null-terminated or non-null-terminated fashion in my_class.
As far as I can see string_view supports a constexpr constructor too.
However this doesn't allow for the signature const char* c_str(). If you are bound to that, the string must be null-terminated in order to allow the caller to retrieve the length (by counting).

