Skip to content

Missing swap for gsl::not_null for move-only types #1129

@christianbrugger

Description

@christianbrugger

I want to exchange the content of two gsl::not_null<std::unique_ptr<int>>.

I was pretty sure a swap would work, but couldn't get it working.

#include <memory>
#include <algorithm>

#include <gsl/gsl>

auto main() -> int {
    
    auto a = gsl::not_null<std::unique_ptr<int>> {std::make_unique<int>(0)};
    auto b = gsl::not_null<std::unique_ptr<int>> {std::make_unique<int>(1)};

    using std::swap;
    swap(a, b);

    return *a;
}


https://godbolt.org/z/64na6e356

This gives an error:

no matching function for call to 'swap(gsl::not_null<std::unique_ptr<int> >&, gsl::not_null<std::unique_ptr<int> >&)'

I understand that its not possible to move from such a pointer.
However, not even enabling swap for smart-pointers makes std::not_null even less useful.

Is there anything against implementing a custom swap, that takes two not_null pointers and swaps the underlying, as we know they are both not null?

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

Status
Fixed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions