Code Monkey home page Code Monkey logo

explicit's Introduction

explicit Build Status

A set of small tools that allow you to state your intentions more explicitly in the interfaces.

Tool out_param

A tool for indicating function output parameters in call sites:

void clear(out_param<std::string&> s) // output parameter in function declaration
{
    s.get().clear();
}

std::string s {"text"};
clear(out(s));                        // in function invokation

For more, see here.

Tool tagged_bool

An alternative to type bool in function arguments. It allows to associate a name with the boolean type. No nasty implicit converisons from/to int, double, or pointers. Different instantiations of tagged_bool are not interconvertible:

class EngineStartedTag_; // never defined
class CrewReadyTag_;     // never defined

using EngineStarted = tagged_bool<EngineStartedTag_>; // one boolean type
using CrewReady     = tagged_bool<CrewReadyTag_>;     // another boolean type

void set_status(EngineStarted started, CrewReady ready); // function declaration
set_status(EngineStarted{true}, CrewReady{true});        // function invokation

For more, see here.

Tool only_when

A tool for disabling some unwanted implicit conversions to types present in your function signatures. For instance, you may want your function to take a filesystem::path but not allow an implicit conversion from std::string:

using only_path = only_when<filesystem::path, is_a_non_string>; // you define type trait is_a_non_string
void process(only_path p);  // function declaration
process(path);    // ok
process(string);  // error

For more, see here.

Tool only_int

An alternative to type int in function arguments. It binds to ints and int proxies but not to double:

void scale(only_int i); // declaration
scale(2);     // ok
scale(2.5);   // error

For more, see here.

Tool lvalue_ref

This allows the constructor parameters to bind to lvalues, but not to rvalues:

struct Processor
{
  Big const& _big;
  explicit Processor(lvalue_ref<const Big> b) : _big(b) {}
};

const Big b {};
Processor p {b}; // ok
Processor q {Big{}}; // error (temporary)

For more, see here.

Tool not_null

This allows to indicate in function interface that a passed pointer is assumed never to be null:

void process(not_null<i*> p); // function declaration
int i = 0;
process(&i);                  // error: no implicit conversion to not_null
process(as_not_null(&i));     // ok: explicit adjustment

For more, see here.

installation

It is a C++11 header-only library.

All library components are defined in namespace ak_toolkit::xplicit. However, including header <ak_toolkit/namespace_xpl> introduces a shorter alias xpl. Using xpl is shorter, but risks clashes in case a different library also uses short xpl for some other purpose. If that happens, you have to go with the longer but safer version.

License

Distributed under the Boost Software License, Version 1.0.

Acknowledgements

The idea of the generalized only_when was proposed by Vicente J. Botet Escriba. Tomasz Kamiński suggested the support for std::reference_wrapper in rvalue_ref.

explicit's People

Contributors

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