Why I can't static_cast char* to std::byte*?

Viewed 987

I know I can use reinterpret_cast, but it seems weird that I can not go from char to "universal" type like std::byte. Is this just unfortunate mistake/limitation, or there is a reason for this?

Example:

int main(){
    std::string s{"abc"};
    std::byte* ptr  = static_cast<std::byte*>(s.data());
}
1 Answers

Static cast only works between:

  1. Numerical types
  2. Possibly related class type pointers/referrnces (up and down).
  3. Pointers to/fom void pointers.
  4. Activating conversion constructors/operators

That is it.

Reinterpreting things as bytes is a reinterpret cast.

Related