Git Product home page Git Product logo

newtype's Introduction

newtype

The newtype library provides types and functions to facilitate the creation of strong type aliases.

C++20 GitHub license Documentation Status travis Download the latest version on conan Support the project with Bitcoin

Usage

The code block below demonstrates the basic usage and features of newtype. For a more details description of the library, as well as a full API documentation, please read the docs (also available as a PDF file).

#include <newtype/derivable.hpp>
#include <newtype/deriving.hpp>
#include <newtype/new_type.hpp>

#include <iostream>

using Width = nt::new_type<unsigned int, struct width_tag, deriving(nt::Read)>;
using Height = nt::new_type<unsigned int, struct height_tag, deriving(nt::Read)>;
using Area = nt::new_type<unsigned int, struct area_tag, deriving(nt::Show)>;

struct Rectangle
{
  constexpr Rectangle(Width w, Height h)
      : width{w}
      , height{h}
  {
  }

  auto constexpr area() const noexcept -> Area
  {
    return {width.decay() * height.decay()};
  }

private:
  Width width;
  Height height;
};

int main()
{
  auto width = Width{};
  auto height = Height{};

  std::cin >> width >> height;

  auto rect = Rectangle{width, height};

  std::cout << rect.area() << '\n';
}

Requirements

This library uses features of C++20 and thus requires a modern compiler. All development was done on GCC 9.2. This is a header-only library, and thus no compilation is need if you want to use it in your project. If you want to run the sanity-checks/unit-test, you will need at least CMake 3.9.0. If you want to build to documentation, you will need either a local installation of sphinx, or alternatively pipenv. A Pipfile is provided in the directory docs within the source root.

newtype's People

Contributors

fmorgner avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.