noexcept(typename std::decay<T>::type(std::forward<T>(value)))) {
return std::forward<T>(value);
}
+
+/**
+ * A simple helper for getting a constant reference to an object.
+ *
+ * Example:
+ *
+ * std::vector<int> v{1,2,3};
+ * // The following two lines are equivalent:
+ * auto a = const_cast<const std::vector<int>&>(v).begin();
+ * auto b = folly::as_const(v).begin();
+ *
+ * Like C++17's std::as_const. See http://wg21.link/p0007
+ */
+#if __cpp_lib_as_const || _MSC_VER
+
+/* using override */ using std::as_const
+
+#else
+
+template <class T>
+constexpr T const& as_const(T& t) noexcept {
+ return t;
+}
+
+template <class T>
+void as_const(T const&&) = delete;
+
+#endif
}
EXPECT_FALSE(noexcept(folly::copy(thr)));
EXPECT_TRUE(noexcept(folly::copy(std::move(thr)))); // note: does not copy
}
+
+TEST_F(UtilityTest, as_const) {
+ struct S {
+ bool member() {
+ return false;
+ }
+ bool member() const {
+ return true;
+ }
+ };
+ S s;
+ EXPECT_FALSE(s.member());
+ EXPECT_TRUE(folly::as_const(s).member());
+ EXPECT_EQ(&s, &folly::as_const(s));
+ EXPECT_TRUE(noexcept(folly::as_const(s)));
+}