Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(494)

Side by Side Diff: base/basictypes.h

Issue 77343005: When using C++11, use static_assert to implement COMPILE_ASSERT (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: git cl try Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_BASICTYPES_H_ 5 #ifndef BASE_BASICTYPES_H_
6 #define BASE_BASICTYPES_H_ 6 #define BASE_BASICTYPES_H_
7 7
8 #include <limits.h> // So we can set the bounds of our types 8 #include <limits.h> // So we can set the bounds of our types
9 #include <stddef.h> // For size_t 9 #include <stddef.h> // For size_t
10 #include <string.h> // for memcpy 10 #include <string.h> // for memcpy
11 11
12 #include "base/compiler_specific.h"
12 #include "base/port.h" // Types that only need exist on certain systems 13 #include "base/port.h" // Types that only need exist on certain systems
13 14
14 #ifndef COMPILER_MSVC 15 #ifndef COMPILER_MSVC
15 // stdint.h is part of C99 but MSVC doesn't have it. 16 // stdint.h is part of C99 but MSVC doesn't have it.
16 #include <stdint.h> // For intptr_t. 17 #include <stdint.h> // For intptr_t.
17 #endif 18 #endif
18 19
19 typedef signed char schar; 20 typedef signed char schar;
20 typedef signed char int8; 21 typedef signed char int8;
21 typedef short int16; 22 typedef short int16;
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 // content_type_names_incorrect_size); 209 // content_type_names_incorrect_size);
209 // 210 //
210 // or to make sure a struct is smaller than a certain size: 211 // or to make sure a struct is smaller than a certain size:
211 // 212 //
212 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large); 213 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
213 // 214 //
214 // The second argument to the macro is the name of the variable. If 215 // The second argument to the macro is the name of the variable. If
215 // the expression is false, most compilers will issue a warning/error 216 // the expression is false, most compilers will issue a warning/error
216 // containing the name of the variable. 217 // containing the name of the variable.
217 218
219 #undef COMPILE_ASSERT
220
221 #if __cplusplus >= 201103L
222
223 // Under C++11, just use static_assert.
224 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg)
225
226 #else
227
218 template <bool> 228 template <bool>
219 struct CompileAssert { 229 struct CompileAssert {
220 }; 230 };
221 231
222 #undef COMPILE_ASSERT
223 #define COMPILE_ASSERT(expr, msg) \ 232 #define COMPILE_ASSERT(expr, msg) \
224 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] 233 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] ALLOW_UNUSED
225 234
226 // Implementation details of COMPILE_ASSERT: 235 // Implementation details of COMPILE_ASSERT:
227 // 236 //
228 // - COMPILE_ASSERT works by defining an array type that has -1 237 // - COMPILE_ASSERT works by defining an array type that has -1
229 // elements (and thus is invalid) when the expression is false. 238 // elements (and thus is invalid) when the expression is false.
230 // 239 //
231 // - The simpler definition 240 // - The simpler definition
232 // 241 //
233 // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1] 242 // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
234 // 243 //
(...skipping 22 matching lines...) Expand all
257 // (They seem to think the ">" in "5 > 0" marks the end of the 266 // (They seem to think the ">" in "5 > 0" marks the end of the
258 // template argument list.) 267 // template argument list.)
259 // 268 //
260 // - The array size is (bool(expr) ? 1 : -1), instead of simply 269 // - The array size is (bool(expr) ? 1 : -1), instead of simply
261 // 270 //
262 // ((expr) ? 1 : -1). 271 // ((expr) ? 1 : -1).
263 // 272 //
264 // This is to avoid running into a bug in MS VC 7.1, which 273 // This is to avoid running into a bug in MS VC 7.1, which
265 // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1. 274 // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
266 275
276 #endif
267 277
268 // bit_cast<Dest,Source> is a template function that implements the 278 // bit_cast<Dest,Source> is a template function that implements the
269 // equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in 279 // equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in
270 // very low-level functions like the protobuf library and fast math 280 // very low-level functions like the protobuf library and fast math
271 // support. 281 // support.
272 // 282 //
273 // float f = 3.14159265358979; 283 // float f = 3.14159265358979;
274 // int i = bit_cast<int32>(f); 284 // int i = bit_cast<int32>(f);
275 // // i = 0x40490fdb 285 // // i = 0x40490fdb
276 // 286 //
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 368
359 // Use these to declare and define a static local variable (static T;) so that 369 // Use these to declare and define a static local variable (static T;) so that
360 // it is leaked so that its destructors are not called at exit. If you need 370 // it is leaked so that its destructors are not called at exit. If you need
361 // thread-safe initialization, use base/lazy_instance.h instead. 371 // thread-safe initialization, use base/lazy_instance.h instead.
362 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \ 372 #define CR_DEFINE_STATIC_LOCAL(type, name, arguments) \
363 static type& name = *new type arguments 373 static type& name = *new type arguments
364 374
365 } // base 375 } // base
366 376
367 #endif // BASE_BASICTYPES_H_ 377 #endif // BASE_BASICTYPES_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698