diff options
author | saurabh singh <saurabh8c@gmail.com> | 2017-05-31 08:38:12 +0530 |
---|---|---|
committer | Neil MacIntosh <neilmac@microsoft.com> | 2017-05-30 20:08:12 -0700 |
commit | 30595c1f1d8bd481b2a7658323fa42e5f9a44029 (patch) | |
tree | dd4378d184f096d2df04e0853d8214d7e5a23038 | |
parent | 23581d4d63e6ed41c9e3a3886f24d526dd7a557d (diff) | |
download | platform_external_Microsoft-GSL-30595c1f1d8bd481b2a7658323fa42e5f9a44029.tar.gz platform_external_Microsoft-GSL-30595c1f1d8bd481b2a7658323fa42e5f9a44029.tar.bz2 platform_external_Microsoft-GSL-30595c1f1d8bd481b2a7658323fa42e5f9a44029.zip |
Restricting usage of owner<T> to pointer types (#507)
* Restricting usage of owner<T> to pointer types
* Removing an additional type that was created for testing
* Added comment about the new constraint on owner
* Adding dereference operator to not_null
* Removing dereference operator changes for not-null
* Removing dereference operator changes for not-null
* Review comments
-rw-r--r-- | include/gsl/gsl | 12 | ||||
-rw-r--r-- | tests/owner_tests.cpp | 12 |
2 files changed, 23 insertions, 1 deletions
diff --git a/include/gsl/gsl b/include/gsl/gsl index 65ce7eb..bc46aac 100644 --- a/include/gsl/gsl +++ b/include/gsl/gsl @@ -27,6 +27,7 @@ #include <iostream> #include <memory> +#include <type_traits> #if defined(_MSC_VER) && _MSC_VER < 1910 #pragma push_macro("constexpr") @@ -53,7 +54,16 @@ namespace gsl using std::unique_ptr; using std::shared_ptr; -template <class T> +// +// owner +// +// +// owner<T> is designed as a bridge for code that must deal directly with owning pointers for some reason +// +// T must be a pointer type +// - disallow construction from any type other than pointer type +// +template <class T, class = std::enable_if_t<std::is_pointer<T>::value>> using owner = T; // diff --git a/tests/owner_tests.cpp b/tests/owner_tests.cpp index b719b13..459c646 100644 --- a/tests/owner_tests.cpp +++ b/tests/owner_tests.cpp @@ -20,6 +20,8 @@ #include <functional> +#include <memory> + using namespace gsl; SUITE(owner_tests) @@ -34,6 +36,16 @@ SUITE(owner_tests) CHECK(*p == 121); delete p; } + + TEST(check_pointer_constraint) + { + #ifdef CONFIRM_COMPILATION_ERRORS + { + owner<int> integerTest = 10; + owner<std::shared_ptr<int>> sharedPtrTest(new int(10)); + } + #endif + } } int main(int, const char* []) { return UnitTest::RunAllTests(); } |