Chromium Code Reviews| Index: util/misc/implicit_cast.h |
| diff --git a/util/misc/implicit_cast.h b/util/misc/implicit_cast.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..623d8d1c98569d52feb628a01102bf7a65b8381e |
| --- /dev/null |
| +++ b/util/misc/implicit_cast.h |
| @@ -0,0 +1,39 @@ |
| +// Copyright 2015 The Crashpad Authors. All rights reserved. |
|
scottmg
2015/09/11 23:57:34
Needs to be added to util/util.gyp.
danakj
2015/09/12 00:10:58
Done.
|
| +// |
| +// Licensed under the Apache License, Version 2.0 (the "License"); |
| +// you may not use this file except in compliance with the License. |
| +// You may obtain a copy of the License at |
| +// |
| +// http://www.apache.org/licenses/LICENSE-2.0 |
| +// |
| +// Unless required by applicable law or agreed to in writing, software |
| +// distributed under the License is distributed on an "AS IS" BASIS, |
| +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| +// See the License for the specific language governing permissions and |
| +// limitations under the License. |
| + |
| +namespace crashpad { |
|
scottmg
2015/09/11 23:57:34
This file needs header guards.
danakj
2015/09/12 00:10:58
Oops. Done.
|
| + |
| +// Use implicit_cast as a safe version of static_cast or const_cast |
| +// for upcasting in the type hierarchy (i.e. casting a pointer to Foo |
| +// to a pointer to SuperclassOfFoo or casting a pointer to Foo to |
| +// a const pointer to Foo). |
| +// When you use implicit_cast, the compiler checks that the cast is safe. |
| +// Such explicit implicit_casts are necessary in surprisingly many |
| +// situations where C++ demands an exact type match instead of an |
| +// argument type convertible to a target type. |
| +// |
| +// The From type can be inferred, so the preferred syntax for using |
| +// implicit_cast is the same as for static_cast etc.: |
| +// |
| +// implicit_cast<ToType>(expr) |
| +// |
| +// implicit_cast would have been part of the C++ standard library, |
| +// but the proposal was submitted too late. It will probably make |
| +// its way into the language in the future. |
| +template<typename To, typename From> |
| +inline To implicit_cast(From const &f) { |
| + return f; |
| +} |
| + |
| +} // namespace crashpad |