[llvm] r175723 - Make Optional<T>'s operator bool 'explicit' in C++11

David Blaikie dblaikie at gmail.com
Wed Feb 20 22:05:57 PST 2013


Author: dblaikie
Date: Thu Feb 21 00:05:57 2013
New Revision: 175723

URL: http://llvm.org/viewvc/llvm-project?rev=175723&view=rev
Log:
Make Optional<T>'s operator bool 'explicit' in C++11

Provides a general way to add 'explicit' for conversion operators (a no-op when
compiling as C++98).

Modified:
    llvm/trunk/include/llvm/ADT/Optional.h
    llvm/trunk/include/llvm/Support/Compiler.h

Modified: llvm/trunk/include/llvm/ADT/Optional.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Optional.h?rev=175723&r1=175722&r2=175723&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/Optional.h (original)
+++ llvm/trunk/include/llvm/ADT/Optional.h Thu Feb 21 00:05:57 2013
@@ -86,7 +86,7 @@ public:
   const T& getValue() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
   T& getValue() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
 
-  operator bool() const { return hasVal; }
+  LLVM_EXPLICIT operator bool() const { return hasVal; }
   bool hasValue() const { return hasVal; }
   const T* operator->() const { return getPointer(); }
   T* operator->() { return getPointer(); }

Modified: llvm/trunk/include/llvm/Support/Compiler.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Compiler.h?rev=175723&r1=175722&r2=175723&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Compiler.h (original)
+++ llvm/trunk/include/llvm/Support/Compiler.h Thu Feb 21 00:05:57 2013
@@ -341,4 +341,14 @@
 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
 #endif
 
+/// \macro LLVM_EXPLICIT
+/// \brief Expands to explicit on compilers which support explicit conversion
+/// operators. Otherwise expands to nothing.
+#if (__has_feature(cxx_explicit_conversions) \
+     || defined(__GXX_EXPERIMENTAL_CXX0X__))
+#define LLVM_EXPLICIT explicit
+#else
+#define LLVM_EXPLICIT
+#endif
+
 #endif





More information about the llvm-commits mailing list