Skip to content
ZisIsNotZisPublic

About

Completed: Experimental single-header C++ wrapper for ownership, borrowing, copying, and moves.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C++ Borrowing Class

Completed: A single-header C++ experiment with a runnable example and focused checks for borrowing, copying, ownership transfer, and empty owners. It is not a general-purpose borrow checker: callers must keep borrowed objects alive and avoid unchecked access to an empty owner. See the completion record for the verified result and limits.

Usage

To try it, get the source from Code → Download ZIP, or run:

git clone https://github.com/ZisIsNotZis/cppBorrow.git
cd cppBorrow

In the source directory, with a C++17-capable GCC, save this example as example.cpp next to borrow.hpp. No library build or install is needed.

#include "borrow.hpp"
#include <iostream>
#include <utility>

struct resource { int value; };

int main() {
    box<resource> empty;             // Null; safe to destroy, not to dereference.
    box<resource> owner(resource{41});
    {
        auto view = owner.borrow();  // Same object; the view does not own it.
        (*view).value = 42;
    }
    auto duplicate = owner.copy();   // A distinct resource object.
    (*duplicate).value = 7;
    auto next = owner.move();        // owner is now empty.
    box<resource> final_owner(std::move(next));  // next is now empty, too.
    std::cout << (*final_owner).value << ' ' << (*duplicate).value << '\n';
}
$ g++ -std=c++17 example.cpp -o example
$ ./example
42 7

Contract and limits

  • Borrow: The compiler rejects .move() on box<T, false> because the member does not exist. It does not enforce lifetimes: a view or reference must not be used after its pointee dies. Transferring an owner does not relocate the pointee; any existing views still depend on the receiving owner keeping it alive. Do not borrow from a temporary owner or return a view of a local owner.
  • Copy: .copy() copies the underlying T into a distinct object. Whether any internal data remains shared is up to T's copy semantics. When wrapping a resource directly, a mutable lvalue or rvalue is moved from; a const lvalue is copied. If you want to keep a mutable input unchanged, pass it as std::as_const(input) (C++17).
  • Transfer: .move() and construction from std::move(owner) (for a non-const owner) transfer the same object and leave the source null. Use these spellings for transfer and .copy() for duplication. In this experimental API, direct construction from a mutable owner lvalue also steals ownership, despite looking like a copy; construction from a const owner is not a supported way to copy arbitrary T. The owner has no assignment operator.
  • Raw pointer: box<T>(ptr) adopts the pointer, rather than borrowing it. For a non-null pointer, hand it a single object that this box may delete; do not pass a stack object, an array, or a pointer another owner will delete.
  • Empty: A default-constructed owner, an owner constructed with a null T*, and a transferred-from owner can all be destroyed safely. On an empty owner, do not use operator*, .borrow(), or .copy(): those operations require a live pointee and do not check or throw on empty access. A default-constructed view does not establish a usable reference.

Checks

Run sh check.sh here with GCC available. It compiles and runs the README example and focused ownership tests, checks that .move() on a view is rejected, and compiles (but never executes) two examples that escape the pointee lifetime. The default check was verified with GCC 15.3.0; other compilers and arbitrary T types are not covered. For an optional regression comparison, point sh check.sh --baseline /path/to/checkout at the original revision 01f9707f55430c9a3b87bf06c631a26c9fd26dcb. The test clears the old duplicate pointer before either owner is destroyed; it does not execute the old uninitialized default-owner path.

Feedback and reuse

Report reproducible issues on GitHub Issues. This repository does not currently specify a license; ask the maintainer before reusing or redistributing the code.

About

Completed: Experimental single-header C++ wrapper for ownership, borrowing, copying, and moves.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages