Git Product home page Git Product logo

strong's Introduction

strong

A C++ library for creating strong (aka opaque) typedefs

When should I use strong typedefs?

Strong typedefs prevent (potentially dangerous) implicit conversions when dealing with custom types. The default typedefs provided by C and C++ are not sufficient for type safety. A custom class that wraps a plain-old-data type can provide that type safety. There is an excellent talk on the subject from CppCon2015 by Kyle Markley. More motivation can be found in a blog post about type safety by Jonathan Müller.

Should I use this library?

I created this library to teach myself more about strong typedefs and how they are implemented in C++. I would not recommend using this library in its current form. The aforementioned names, Kyle Markley and Jonathan Müller, each have their own (more complete) libraries for strong typedefs that you can check out here and here.

Using this library (compilation)

This is a header-only library, so you can simply copy the header file into your project to use it. Alternatively, you can install it or add it as a subdirectory to your project and then use CMake. To use CMake, add the subdirectory and then use target_link_libraries with strong.

Creating a strong type

#include <strong.hpp>
#include <iostream>

struct my_type : strong::type<my_type, int>
{
  // inherit the base class's constructors
  using strong::type<my_type, int>::type;
}

int main()
{
  my_type test(52);
  
  // we can access the underlying value with the get function
  std::cout << strong::get(test) << "\n";
  
  return 0;
}

strong's People

Contributors

mariobadr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.