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
This commit is contained in:
saurabh singh 2017-05-31 08:38:12 +05:30 committed by Neil MacIntosh
parent 23581d4d63
commit 30595c1f1d
2 changed files with 23 additions and 1 deletions

View File

@ -27,6 +27,7 @@
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <type_traits>
#if defined(_MSC_VER) && _MSC_VER < 1910 #if defined(_MSC_VER) && _MSC_VER < 1910
#pragma push_macro("constexpr") #pragma push_macro("constexpr")
@ -53,7 +54,16 @@ namespace gsl
using std::unique_ptr; using std::unique_ptr;
using std::shared_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; using owner = T;
// //

View File

@ -20,6 +20,8 @@
#include <functional> #include <functional>
#include <memory>
using namespace gsl; using namespace gsl;
SUITE(owner_tests) SUITE(owner_tests)
@ -34,6 +36,16 @@ SUITE(owner_tests)
CHECK(*p == 121); CHECK(*p == 121);
delete p; 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(); } int main(int, const char* []) { return UnitTest::RunAllTests(); }