Chromium Code Reviews| Index: base/debug/crash_logging.h |
| diff --git a/base/debug/crash_logging.h b/base/debug/crash_logging.h |
| index 97bac754a4b626985fe26935d37de8008fb7f7d7..0f9e1148034491a5d70271961492657c3dbac89c 100644 |
| --- a/base/debug/crash_logging.h |
| +++ b/base/debug/crash_logging.h |
| @@ -49,12 +49,35 @@ class BASE_EXPORT ScopedCrashKey { |
| ScopedCrashKey(const base::StringPiece& key, const base::StringPiece& value); |
| ~ScopedCrashKey(); |
| + // Helper to force a static_assert when instantiating a ScopedCrashKey |
| + // temporary without a name. The usual idiom is to just #define a macro that |
| + // static_asserts with the message; however, that doesn't work well when the |
| + // type is in a namespace. |
| + // |
| + // Instead, we use a templated helper to trigger the static_assert, observing |
| + // two rules: |
| + // - The static_assert needs to be in a normally uninstantiated template; |
| + // otherwise, it will fail to compile =) |
| + // - Similarly, the static_assert must be dependent on the template argument, |
| + // to prevent it from being evaluated until the template is instantiated. |
| + template<typename T> |
| + ScopedCrashKey(const T&) { |
|
danakj
2016/07/12 19:59:26
Why a constructor and not just a free CrashFromBad
dcheng
2016/07/13 00:34:11
This prevents me from having to "using base::debug
Robert Sesek
2016/07/13 19:46:22
What about something like this:
enum class Unname
danakj
2016/07/13 19:48:16
I was just thinking similar things.. but more like
|
| + static_assert( |
| + sizeof(T) == 0, |
| + "scoped crash key objects should not be unnamed temporaries."); |
| + } |
| + |
| private: |
| std::string key_; |
| DISALLOW_COPY_AND_ASSIGN(ScopedCrashKey); |
| }; |
| +// Disallow an instantation of ScopedCrashKey without a name, since this results |
| +// in a temporary that is immediately destroyed. Doing so will trigger the |
| +// static_assert in the templated constructor helper in ScopedCrashKey. |
| +#define ScopedCrashKey(key, value) ScopedCrashKey(0xbad) |
| + |
| // Before setting values for a key, all the keys must be registered. |
| struct BASE_EXPORT CrashKey { |
| // The name of the crash key, used in the above functions. |