diff options
author | Anna Gringauze <annagrin@microsoft.com> | 2018-06-07 13:36:56 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-07 13:36:56 -0700 |
commit | cb2d1af89afb76bf50efe8e068c5ea1fc2872274 (patch) | |
tree | 88464c546dbcbcb1c093d3a19f47e0b080f99a51 /tests/notnull_tests.cpp | |
parent | 75ad0c1b40d27a2d0749861df923cb38f866a6c2 (diff) | |
download | platform_external_Microsoft-GSL-cb2d1af89afb76bf50efe8e068c5ea1fc2872274.tar.gz platform_external_Microsoft-GSL-cb2d1af89afb76bf50efe8e068c5ea1fc2872274.tar.bz2 platform_external_Microsoft-GSL-cb2d1af89afb76bf50efe8e068c5ea1fc2872274.zip |
Added template argument deduction for not_null (#689)
* Added template argument deduction for not_null
This allows compilers with c++17 support to infer template
instantiation types when calling not_null constructor:
int foo(not_null<const int*> x);
int main()
{
int t = 0;
not_null x{ &t };
return foo(not_null{ &t });
}
* replaced deduction guides by a simple constructor
* Updated tests
* fixed check for availability of std::byte
* testing c++1z on clang
* fixed cmakelists extra endif
* include cstddef header for clang and gcc in pointers
* fixed a typo
* fix missing nullptr_t type
* fixed typo in CMakeLists.tst
* change approach to c++17 testing, step one: revert cmake testing, update clang5.0 package
removed using latest c++ due to clang5.0 failing,
update clang5.0 travis config to use llvm-toolchain-trusty-5.0
* addressed comments
Diffstat (limited to 'tests/notnull_tests.cpp')
-rw-r--r-- | tests/notnull_tests.cpp | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/notnull_tests.cpp b/tests/notnull_tests.cpp index fa99bb7..1cb9c10 100644 --- a/tests/notnull_tests.cpp +++ b/tests/notnull_tests.cpp @@ -112,6 +112,7 @@ struct NonCopyableNonMovable }; bool helper(not_null<int*> p) { return *p == 12; } +bool helper_const(not_null<const int*> p) { return *p == 12; } TEST_CASE("TestNotNullConstructors") { @@ -328,4 +329,62 @@ TEST_CASE("TestNotNullCustomPtrComparison") CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1)); } + +#if defined(__cplusplus) && (__cplusplus >= 201703L) +TEST_CASE("TestNotNullConstructorTypeDeduction") +{ + { + int i = 42; + + not_null x{&i}; + helper(not_null{&i}); + helper_const(not_null{&i}); + + CHECK(*x == 42); + } + + { + int i = 42; + int* p = &i; + + not_null x{p}; + helper(not_null{p}); + helper_const(not_null{p}); + + CHECK(*x == 42); + } + + { + auto workaround_macro = []() { + int* p1 = nullptr; + not_null x{p1}; + }; + CHECK_THROWS_AS(workaround_macro(), fail_fast); + } + + { + auto workaround_macro = []() { + const int* p1 = nullptr; + not_null x{p1}; + }; + CHECK_THROWS_AS(workaround_macro(), fail_fast); + } + + { + int* p = nullptr; + + CHECK_THROWS_AS(helper(not_null{p}), fail_fast); + CHECK_THROWS_AS(helper_const(not_null{p}), fail_fast); + } + +#ifdef CONFIRM_COMPILATION_ERRORS + { + not_null x{nullptr}; + helper(not_null{nullptr}); + helper_const(not_null{nullptr}); + } +#endif +} +#endif // #if defined(__cplusplus) && (__cplusplus >= 201703L) + static_assert(std::is_nothrow_move_constructible<not_null<void *>>::value, "not_null must be no-throw move constructible"); |