Make Optional<T>'s operator bool 'explicit' in C++11
authorDavid Blaikie <dblaikie@gmail.com>
Thu, 21 Feb 2013 06:05:57 +0000 (06:05 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Thu, 21 Feb 2013 06:05:57 +0000 (06:05 +0000)
Provides a general way to add 'explicit' for conversion operators (a no-op when
compiling as C++98).

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@175723 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/Optional.h
include/llvm/Support/Compiler.h

index 844e3090500665771450b07854c0b93803429971..c5dc29946cb8dcd498e0f6b5b1b8a53a53ae655a 100644 (file)
@@ -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(); }
index adc0ce2ab14490d34fd6aea2fb45fcc3b34530a0..25f42a98e7dec07caa0d5275374c6760ef85ea9a 100644 (file)
 # 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