# \<scope\> library

**URL:** <https://discourse.bemanproject.org/t/scope-library/315>\
**Category:** Library::scope\
**Created:** [January 22, 2025, 1:16pm UTC](https://discourse.bemanproject.org/t/scope-library/315 "2025-01-22T13:16:52Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Robert-Andrzejuk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/robert-andrzejuk/32/197_2.png) [@Robert-Andrzejuk](https://discourse.bemanproject.org/u/Robert-Andrzejuk)\
**Post date:** [February 6, 2025, 5:33pm UTC](https://discourse.bemanproject.org/t/scope-library/315/21 "2025-02-06T17:33:54Z")

</div>

Here is a gist of how the scope\_guard machinary could be written:

> <https://gist.github.com/robert-andrzejuk/ad07ddcde22d3ffbce167e57e8530889#file-scope_guard-hpp>

---

<div class="post-metadata">

**Author:** ![Jeff-Garland](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/jeff-garland/32/23_2.png) [@Jeff-Garland](https://discourse.bemanproject.org/u/Jeff-Garland)\
**Post date:** [February 7, 2025, 10:57am UTC](https://discourse.bemanproject.org/t/scope-library/315/22 "2025-02-07T10:57:06Z")

</div>

Yes, looks like a good first approximation. Looking at specification from the TS (aka P0052) – and off the top of my head maybe there maybe needs to be:

- constexpr on various functions (not in TS, but think should be now?)
- remove iostream include
- header should be scope - no `_guard` (I wonder if in beman it should be without .hpp like standard?)
- constructors are explicit in places in TS - aka:

```auto
template<class EFP> explicit scope_guard (EFP&& f) 

```

- there’s some conditional noexcept logic on `scope_success` (also `scope_guard` – more below)

```auto
scope_success() noexcept(noexcept(exit_function()));

```

- probably should explicitly disable assignment like in spec – should be anyway I think, but can’t hurt

```auto
scope_guard (const scope_guard &)=delete;
scope_guard & operator=(const scope_guard &)=delete;
scope_guard & operator=(scope_guard &&)=delete;

```

- `scope_guard` should be in a detail namespace - it’s not part of the api (more below)

Note that a _pro-trick_ when I’m evaluating wording, I copy the synopsis from the paper and work with it until it compiles or doesn’t – so many bugs in wording discovered this way. My point being that what we’re doing here is a _backward_ check of the specification by attempting to implement it.

So my 15 minute check of _your implementation_ against the spec shines the light on at least a couple specification problems from P0052. Specifically

```auto
~scope_guard () noexcept(see below )

```

There’s no text corresponding to _see below_ – in fact there’s almost no specification of `scope_guard` itself.

The intent in the wording is that `scope_guard` is _exposition-only_ – that is not available to end users (hence my detail namespace comment above). But you don’t know that because it’s not marked properly as exposition only. Also in the wording it should be called _scope-guard_ (uncompilable kebob case) - it’s _just in italics_…

Anyway, I think you should turn this draft into an initial PR on top of @river 's.

---

<div class="post-metadata">

**Author:** ![Robert-Andrzejuk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/robert-andrzejuk/32/197_2.png) [@Robert-Andrzejuk](https://discourse.bemanproject.org/u/Robert-Andrzejuk)\
**Post date:** [February 7, 2025, 11:53am UTC](https://discourse.bemanproject.org/t/scope-library/315/23 "2025-02-07T11:53:59Z")

</div>

Thank you for the information.  
My implementation was just to get the example working… there are still many things wrong with it, to make it work in all cases.

I think the correct mindset would be: we are reverse engineering the paper.  
I will get on it.

---

<div class="post-metadata">

**Author:** ![Jeff-Garland](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/jeff-garland/32/23_2.png) [@Jeff-Garland](https://discourse.bemanproject.org/u/Jeff-Garland)\
**Post date:** [February 7, 2025, 12:38pm UTC](https://discourse.bemanproject.org/t/scope-library/315/24 "2025-02-07T12:38:22Z")

</div>

I’ve started adding issues to the repo to document stuff we’re finding here so it can go into the paper. I’ll be traveling to Europe later today for the Hagenberg c++ meeting so I might be able to get a draft outline for the paper in place. I don’t know if Peter will be there, but I’ll be reaching out to him and the Boost author soon to see if they want to participate.

---

<div class="post-metadata">

**Author:** ![Robert-Andrzejuk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/robert-andrzejuk/32/197_2.png) [@Robert-Andrzejuk](https://discourse.bemanproject.org/u/Robert-Andrzejuk)\
**Post date:** [February 11, 2025, 10:26am UTC](https://discourse.bemanproject.org/t/scope-library/315/25 "2025-02-11T10:26:23Z")

</div>

An alternative to the synopsis that I provided earlier is now in: [GitHub - bemanproject/scope at scope-guard-demo](https://github.com/bemanproject/scope/tree/scope-guard-demo)  
Available in: include/beman/scope/scope\_guard\_demo.hpp  
Also the example in this branch has been modified to demonstrate how this demo can be used.

The synopsis has already been copied into the main branch, I’m working on making the intial implementaion. The demo code can be used as basis, but it requires a change - from using constructor parameters to using template parameters.

Please review the infrastructure pulls.

---

<div class="post-metadata">

**Author:** ![Robert-Andrzejuk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/robert-andrzejuk/32/197_2.png) [@Robert-Andrzejuk](https://discourse.bemanproject.org/u/Robert-Andrzejuk)\
**Post date:** [February 18, 2025, 12:42am UTC](https://discourse.bemanproject.org/t/scope-library/315/26 "2025-02-18T00:42:46Z")

</div>

> - exception header
> 
> Since scope is fundamentally about dealing with exceptions I think that is a requirement.

I might be overthinking this.

The way I see it, a `scope_guard` does not deal with exceptions by itself.  
Reacting to exceptions is a customization of a `scope_guard`.  
Similarly, making a `scope_guard` cancelable or releasable is another customization.  
These customizations can be applied individually or together, or even with other custom conditions.

As I see it, a scope\_guard consists of:

- an initialization step: creating the scope\_guard object,
- a checking step: verifying if conditions are met to execute the finalization function,
- a finalization step: executing previously deferred functions if the above check is fulfilled.

`scope_fail` and `scope_success` are just convenience classes, which are essentially preconfigured `scope_guards`.

---

<div class="post-metadata">

**Author:** ![Jeff-Garland](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/jeff-garland/32/23_2.png) [@Jeff-Garland](https://discourse.bemanproject.org/u/Jeff-Garland)\
**Post date:** [February 19, 2025, 4:42pm UTC](https://discourse.bemanproject.org/t/scope-library/315/27 "2025-02-19T16:42:00Z")

</div>

So in the end `scope_guard` isn’t part of the public interface because it’s marked exposition only in the TS. Hence _the only relevant question_ is do the 3 public interface classes need exceptions – they do and hence my point. What it means is that implementations do not need to create a `scope_guard` at all – they can just code the 3 types. So users _are NOT free_ to derive to make their own, etc. As I think I put in the design issue I this should be reconsidered I think.

I guess even if `scope_guard` wasn’t this way it’s in the same header as the others so exception is still needed. And in 3 years the world will start shifting to `import std;` and it’ll all be water under the bridge.

---

<div class="post-metadata">

**Author:** ![Jeff-Garland](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/jeff-garland/32/23_2.png) [@Jeff-Garland](https://discourse.bemanproject.org/u/Jeff-Garland)\
**Post date:** [March 10, 2025, 12:49pm UTC](https://discourse.bemanproject.org/t/scope-library/315/28 "2025-03-10T12:49:46Z")

</div>

I’ve archived the original repo - @Robert-Andrzejuk you seem to have a fork of it, but there’s little activity. I expect to simply delete it soon.

> **[GitHub - bemanproject/scope-deprecated: Deprecated repo -- please see beman/scope](https://github.com/bemanproject/scope-deprecated/)**
>
> Deprecated repo -- please see beman/scope

---

<div class="post-metadata">

**Author:** ![Jeff-Garland](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/jeff-garland/32/23_2.png) [@Jeff-Garland](https://discourse.bemanproject.org/u/Jeff-Garland)\
**Post date:** [May 9, 2025, 12:46pm UTC](https://discourse.bemanproject.org/t/scope-library/315/29 "2025-05-09T12:46:31Z")

</div>

The repo has been deleted.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/clausklein/32/136_2.png) [@ClausKlein](https://discourse.bemanproject.org/u/ClausKlein)\
**Post date:** [January 15, 2026, 10:40am UTC](https://discourse.bemanproject.org/t/scope-library/315/30 "2026-01-15T10:40:32Z")

</div>

what is the use case for [`std::experimental::unique_resource`](https://en.cppreference.com/w/cpp/experimental/unique_resource/get_deleter.html):

```cpp
const D& get_deleter() const noexcept;
// Returns: deleter.

```

---

<div class="post-metadata">

**Author:** ![PeterSommerlad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/petersommerlad/32/334_2.png) [@PeterSommerlad](https://discourse.bemanproject.org/u/PeterSommerlad)\
**Post date:** [January 16, 2026, 6:37am UTC](https://discourse.bemanproject.org/t/scope-library/315/31 "2026-01-16T06:37:31Z")

</div>

Since I was invited to “scope” just a quick remark from my mobile: I abandoned scope guards and especially unique resource, because I think using those is poor practice, where a better resource management abstraction is missing. I therefore intensified my teaching of how to implement Scoped and Unique Manager types instead. I think it would be a mistake to standardize something that leads to sloppy design.

Regards

Peter

---

<div class="post-metadata">

**Author:** ![dsankel](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/dsankel/32/5_2.png) [@dsankel](https://discourse.bemanproject.org/u/dsankel)\
**Post date:** [January 17, 2026, 1:43pm UTC](https://discourse.bemanproject.org/t/scope-library/315/32 "2026-01-17T13:43:28Z")

</div>

> [@PeterSommerlad](#):
>
> I abandoned scope guards and especially unique resource, because I think using those is poor practice, where a better resource management abstraction is missing.

Thanks for chiming in Peter and welcome! Can you elaborate on why you find using scope guards and unique resource as poor practice?

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/clausklein/32/136_2.png) [@ClausKlein](https://discourse.bemanproject.org/u/ClausKlein)\
**Post date:** [January 17, 2026, 8:05pm UTC](https://discourse.bemanproject.org/t/scope-library/315/33 "2026-01-17T20:05:07Z")

</div>

You may found also some critical notes at boost:

> **[Comparison with unique\_resource defined in C++ Extensions for Library... -...](https://www.boost.org/doc/libs/latest/libs/scope/doc/html/scope/unique_resource.html#scope.unique_resource.comparison_with_library_fundamentals_ts)**
>
> The following sections provide comparison between unique\_resource defined by C++ Extensions for Library Fundamentals TS and this library.

---

<div class="post-metadata">

**Author:** ![PeterSommerlad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/petersommerlad/32/334_2.png) [@PeterSommerlad](https://discourse.bemanproject.org/u/PeterSommerlad)\
**Post date:** [January 24, 2026, 9:11am UTC](https://discourse.bemanproject.org/t/scope-library/315/34 "2026-01-24T09:11:37Z")

</div>

Hi David,

deeper thought and teaching C++ design made me come tho my conclusion. I teach C++ classes/types coming in different flavors/kinds: Values, Relation(pointer-like), OO-Polymorphic (virtual), and Managers (defining a destructor with a body). Without going into more detail, Manager types manage a single resources come in three subflavors, depending on movabiltiy/copyability:

- Scoped Managers - non-movable (Rule of DesDeMovA)
- Unique Managers - move-only (i.e. optional as member)
- General Managers - copyable - value types, move as optimization

Since it is very simple and teachable to create a Scoped/Unique Manager type and also much better testable than using a scope guard/unique\_resource I consider the generic scope guards/unique\_resource a hack that hinders testabiltiy and reuse. Having a domain specific manager type is much better design.

Rule of DesDeMovA: define move-assignment as =delete to prevent all copy-move operations with the least amount of code.

My class design recommendations are also reflected in the MISRA-C++:2023 guidelines and in the draft of WG23’s C++ vulnerabilities document.

If you need more details, you can find some talk videos by me from a couple of years, or you can look at the publicly visible teaching material of my C++ trainings (copyrighted) or ask me via email with specific questions.

Regards

Peter

---

<div class="post-metadata">

**Author:** ![dsankel](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/dsankel/32/5_2.png) [@dsankel](https://discourse.bemanproject.org/u/dsankel)\
**Post date:** [February 2, 2026, 4:11pm UTC](https://discourse.bemanproject.org/t/scope-library/315/35 "2026-02-02T16:11:34Z")

</div>

Thanks Peter. If you prefer a domain-specific manager type, wouldn’t it still be more convenient if there were a general facility to build upon?

```auto
class DomainSpecificManager : public std::generic_manager<…> {
  DomainSpecificManager(Resource r)
    : std::generic_manager<…>( [=](){free_resource(r);} )
  {}
};

```

---

<div class="post-metadata">

**Author:** ![PeterSommerlad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/petersommerlad/32/334_2.png) [@PeterSommerlad](https://discourse.bemanproject.org/u/PeterSommerlad)\
**Post date:** [February 20, 2026, 11:00am UTC](https://discourse.bemanproject.org/t/scope-library/315/36 "2026-02-20T11:00:23Z")

</div>

Hi David,

sorry for my latency in replying to your question.

I doubt that it is a benefit wrt teachability and usabilty to provide a  
generic manager class template, since resource allocation should be part  
of the manager class constructor and thus a “generic manager” base  
wouldn’t help much wrt resource representation and allocation. Also  
error handling of the underlying allocation/deallocation might be a bit  
more involved (you don’t want your destructor to throw.)

For example, I just made up a simple ScopedThread manager example that  
is about 12 lines at

> **[Compiler Explorer - C++ (x86-64 gcc 15.2)](https://godbolt.org/z/oeG58rT7e)**
>
> struct ScopedThread {
> template
> ScopedThread(F&& f,P&& ... p)
> : t(std::forward(f),std::forward (p)...){}
> ~ScopedThread(){
> if (t.joinable()){
> t.join(); // terminates on error
> }
> }
> ScopedThread&...

a UniqueThread that I just made up while writing this email is about 20  
lines

> **[Compiler Explorer - C++ (x86-64 gcc 15.2)](https://godbolt.org/z/exvEe15Ed)**
>
> struct UniqueThread {
> template
> UniqueThread(F&& f,P&& ... p)
> : t(std::forward(f),std::forward (p)...){}
> ~UniqueThread(){
> join\_if\_joinable();
> }
> UniqueThread(UniqueThread&&other) noexcept:t{std::move(other.t)}{}
> ...

So I think, teaching C++ programmers how to properly write manager  
classes is much more beneficial than providing a generic, hard to get  
right infrastructure where using it might introduce subtle resource  
leaks, because allocation is not part of a constructor.

Regards  
Peter.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/clausklein/32/136_2.png) [@ClausKlein](https://discourse.bemanproject.org/u/ClausKlein)\
**Post date:** [February 21, 2026, 6:10pm UTC](https://discourse.bemanproject.org/t/scope-library/315/37 "2026-02-21T18:10:12Z")

</div>

`ChatGPT` suggest small improvements?

```cpp
#include <thread>
#include <utility>

class UniqueThread {

public:

    UniqueThread() noexcept = default;

    template<typename F, typename... Args>
    explicit UniqueThread(F&& f, Args&&... args)
        : t(std::forward<F>(f), std::forward<Args>(args)...)
    {}

    ~UniqueThread() noexcept {
        join_if_joinable();
    }

    UniqueThread(UniqueThread&& other) noexcept
        : t(std::move(other.t))
    {}

    UniqueThread& operator=(UniqueThread&& other) noexcept {

        if (this != &other)
        {
            join_if_joinable();
            t = std::move(other.t);
        }

        return *this;
    }

    UniqueThread(const UniqueThread&) = delete;
    UniqueThread& operator=(const UniqueThread&) = delete;

    bool joinable() const noexcept {
        return t.joinable();
    }

    void join() {
        t.join();
    }

    void swap(UniqueThread& other) noexcept {
        t.swap(other.t);
    }

private:

    void join_if_joinable() noexcept {
        if (t.joinable())
            t.join();
    }

    std::thread t;

};

```

---

<div class="post-metadata">

**Author:** ![PeterSommerlad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/petersommerlad/32/334_2.png) [@PeterSommerlad](https://discourse.bemanproject.org/u/PeterSommerlad)\
**Post date:** [February 22, 2026, 8:06am UTC](https://discourse.bemanproject.org/t/scope-library/315/38 "2026-02-22T08:06:56Z")

</div>

in my hurry i forgot the self-assignment check, i should have copied from my slides 🙂 however, implementing swap and defining the deleted functions are completely superfluous imho and should not be taught (swap might be an optimization depending on std::thread’s representation but move operations are sufficient for std::swap to work properly)

Regards  
peter

Peter Sommerladpeter.cpp@sommerlad.ch  
+41-79-432 23 32

---

<div class="post-metadata">

**Author:** ![PeterSommerlad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.bemanproject.org/petersommerlad/32/334_2.png) [@PeterSommerlad](https://discourse.bemanproject.org/u/PeterSommerlad)\
**Post date:** [March 3, 2026, 5:17pm UTC](https://discourse.bemanproject.org/t/scope-library/315/39 "2026-03-03T17:17:23Z")

</div>

Also the omission of the additional functionality suggested was intentional.

If you care about explicitly joining stay with std::thread.

I have chosen that just as an example to document how easy it is to create scoped and unique manager types, and if I had followed my teaching slides I wouldn’t have made the mistake of forgetting to handle self-assignment.

Putting in too much unneeded functionality is what makes standard library types so intricate.

---

<div class="post-metadata">

**Author:** ![VilleVoutilainen](https://avatars.discourse-cdn.com/v4/letter/v/ecae2f/32.png) [@VilleVoutilainen](https://discourse.bemanproject.org/u/VilleVoutilainen)\
**Post date:** [October 1, 2026, 5:26pm UTC](https://discourse.bemanproject.org/t/scope-library/315/41 "2026-10-01T17:26:19Z")

</div>

Ahoi. I would recommend dropping the scope\_success/scope\_fail parts.

Those will cause friction, because they don’t work correctly when used in coroutines and when you resume in a thread different from where you suspended.

unique\_resource alone is fine, and doesn’t have this problem. I don’t think scope\_exit carries its weight (if you have a resource, you can use unique\_resource. If you don’t have a resource, what are you cleaning up?).

See, it’s been on my todo list to do that, to pare the proposal down into essential bits, and look at modernizing it to use deduction guides instead of factory functions, if and when applicable. So I would be happy to help here - but I do think it needs to be pared down into the bare essential(s), where that probably isn’t a plural.

Thanks for listening. Most of you know who I am, so I didn’t add a hello-introduction. 🙂

[Previous page](https://discourse.bemanproject.org/t/scope-library/315.md?page=1)

[Next page](https://discourse.bemanproject.org/t/scope-library/315.md?page=3)
