Giter Site home page Giter Site logo

cppborrow's Introduction

C++ Borrowing Class

Compile-time C++ borrowing mechanism like what's in Rust: Resources will be borrowed, moveed, or copyed instead of ambiguously "passed" everywhere. The mechanism will decide when to free resources. People might not like this since either borrow, move or copy have to be specified for any "passing". But this do makes sure everything will be "safe" at runtime.

Usage

int main()
{
	resource _a;

	box<resource> a(_a);// To `borrow`, `move`, or `copy`, resource have to be wrapped like this.
						// Here, `a` seals resource from `_a`, which means generally `_a` can no longer be used anymore.
						// If `_a` have to be used, write `_a` as `std::cref(_a)` which will result in a deep copy.
						// It is recommended that all resources are wrapped in `box` to avoid C++ swap outside control of `box`.
						// By the way, C++17 might allow class template deduction which means `box a(_a)` will be legal
	
	*a;					// Access wrapped data using `operator*`

	{
		auto b=a.borrow();// `b` borrows resource from `a`. It shares resource with `a` but don't free that.
		( *a ).xxx();		// `a` is still able to access `b`
	}

	{
		auto b=a.move();	// `b` steals resource from `a`. If `a` borrowed resource from somebody else, a compiling
						// error will be given since `a` do not own the resource. Accessing `a` after this will throw
						// a runtime error. (There's no way to make this a compile time error since, this is C++)
						// Also, accessing empty-initialized box will also throw a runtime error. At least everything is
						// well defined.
	}

	{	
		auto b=a.copy();	// `b` owns a duplicate of resource inside `a`. They don't share the same memory space anymore.
	}
    return 0;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.