implicit_cast.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2015 The Crashpad Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef CRASHPAD_UTIL_MISC_IMPLICIT_CAST_H_
  15. #define CRASHPAD_UTIL_MISC_IMPLICIT_CAST_H_
  16. namespace crashpad {
  17. // Use implicit_cast as a safe version of static_cast or const_cast
  18. // for upcasting in the type hierarchy (i.e. casting a pointer to Foo
  19. // to a pointer to SuperclassOfFoo or casting a pointer to Foo to
  20. // a const pointer to Foo).
  21. // When you use implicit_cast, the compiler checks that the cast is safe.
  22. // Such explicit implicit_casts are necessary in surprisingly many
  23. // situations where C++ demands an exact type match instead of an
  24. // argument type convertible to a target type.
  25. //
  26. // The From type can be inferred, so the preferred syntax for using
  27. // implicit_cast is the same as for static_cast etc.:
  28. //
  29. // implicit_cast<ToType>(expr)
  30. //
  31. // implicit_cast would have been part of the C++ standard library,
  32. // but the proposal was submitted too late. It will probably make
  33. // its way into the language in the future.
  34. template <typename To, typename From>
  35. constexpr To implicit_cast(From const& f) {
  36. return f;
  37. }
  38. } // namespace crashpad
  39. #endif // CRASHPAD_UTIL_MISC_IMPLICIT_CAST_H_