Comments (3)
Because C++ doesn't have destructive move, we have to worried about the moved-from state. Although its occurrence cannot be statically prevented¹,
not_null_1<>
makes sure that the moved-from state doesn't proliferate (⇒ it checks fornullptr
in all constructors), and that pointers in moved-from state are not accessed (⇒ it checks fornullptr
in accessor functions such asget()
,as_nullable()
,operator ->()
).
I realized that, for raw pointers, moving just copies, hence there is no dedicated moved-from state to worry about. In fact, the postcondition checks in get()
, as_nullable()
, operator ->()
are unnecessary for not_null_1<T*>
.
That further reduces the necessity for a type with not_null_2<>
semantics; there would be no use for not_null_2<T*>
because not_null_1<T*>
would do exactly the same. That leaves not_null_2<SmartPtr<T>>
, which could act as a semantic substitute for both not_null_1<SmartPtr<T>> const&
and SmartPtr<T> const&
in function arguments. The Core Guidelines give the following advice about passing smart pointers:
R.30: Take smart pointers as parameters only to explicitly express lifetime semantics
F.7: For general use, take T*
or T&
arguments rather than smart pointers
R.36: Take a const shared_ptr<widget>&
parameter to express that it might retain a reference count to the object ???
The shared_ptr<T> const&
use case is arguably a rare corner case, and std::unique_ptr<T> const&
isn't mentioned and thus implicitly discouraged.
So perhaps there isn't much use for not_null_2<>
at all.
from gsl-lite.
I realized that, for raw pointers, moving just copies, hence there is no dedicated moved-from state to worry about. In fact, the postcondition checks in
get()
,as_nullable()
,operator ->()
are unnecessary fornot_null_1<T*>
.
4e0b7d2 addresses this point and adds a specialization of not_null<>
for raw pointer types which elides the unnecessary checks.
from gsl-lite.
Furthermore, if we ever wanted a not_null<>
type with reference/borrow semantics, we could simply make gsl::not_null<>
work for reference type arguments:
// std::size_t compute_hash(std::unique_ptr<Resource> const& res);
std::size_t compute_hash(gsl::not_null<std::unique_ptr<Resource> const&> res);
// "will" or "might" reseat pointer
// void reseat(unique_ptr<widget>&);
void reseat(gsl::not_null<unique_ptr<widget>&>);
// "might" retain refcount
// void may_share(shared_ptr<widget> const&);
void may_share(gsl::not_null<shared_ptr<widget> const&>);
But for now, let's not. Either way, there is really no need for a dedicated type with not_null_2<>
semantics.
from gsl-lite.
Related Issues (20)
- `not_null<>` should be hashable HOT 2
- "illegal instruction" Error when using gsl::span as Kernel Parameter HOT 3
- Test compilation failures on GCC 11: error: array subscript -1 is outside array bounds of 'char[6]' [-Werror=array-bounds] HOT 7
- Test failures on s390x (GCC 10.2.1 and 11.0.1) HOT 7
- Support `narrow<>()` for types without total order
- `not_null<shared_ptr<T>>` not convertible to `weak_ptr<T>` HOT 1
- gsl_FEATURE_GSL_LITE_NAMESPACE not working HOT 3
- Definition of `fail_fast` and of `narrowing_error` triggers `clang`'s `-Wweak-vtables` HOT 2
- gsl_CONFIG_UNENFORCED_CONTRACTS_ASSUME in kernel code HOT 1
- including gsl-lite triggers `-Wuseless-cast` warnings HOT 2
- Add tests for `not_null_ic<>`
- Tests fail to compile due to -Werror=type-limits when char is unsigned HOT 6
- Add CI for non-x86 architectures
- Allow `not_null<void*>` and related. HOT 9
- `not_null`'s evaluation of nullness can lead to *very* cryptic errors. HOT 10
- Suppress warnings in lest via system include
- Comparison of `std::optional<not_null<T>>` and `not_null<T>` is ambiguous HOT 2
- gsl::span works differently than std::span in terms of type matching? HOT 2
- 0.41.0: not ready to be build with `-D_FORTIFY_SOURCE=3` HOT 17
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gsl-lite.