Giter VIP home page Giter VIP logo

vector_constrained's Introduction

Intro

It has come to my attention recently where I would really like to use a constrained vector type. Since C++ is typed language it does not make any sense to often validate the input data into a function for example. An input parameter in a function should be the correct type and shouldn't need validation, in my opinion.

Objective

std::vector wrapper with additional template argument declaring the constraining relationship to the vector. Seamless integration to current code, so you shouldn't need to put much effort in refactoring.

Usage

All of the below code is from the example.cpp file.

Declare a constraining relationship

Make a struct with a callable bool operator,

struct constrain_less_than_20 {
    bool operator()(int value) {
        return value < 20;
    }
};
using lessthan20 = nonstd::vector_constrained<int, std::allocator<int>, constrain_less_than_20>;

Construct vector_constrained from std::vector

std::vector<int> normal_vector{10, 11, 12, 13, 14};
lessthan20 third_vector{normal_vector};

Most of std::vector member functions are implemented

lessthan20 second_vector{first_vector};// copy the first_vector
second_vector.assign({10, 11});// overwrite vector values
second_vector.insert(second_vector.end(), {12, 13});
second_vector.push_back(14);

Implicit conversion to std::vector

void print(const std::vector<int>& vec) {
    for (const auto& item : vec) {
        printf("Item is %d\n", item);
    }
}
int main() {
    lessthan20 third_vector{normal_vector};
    print(third_vector);
}

Known flaws

  • All functions in std::vector where iterator is returned has either been commented or changed to const_iterator.

vector_constrained's People

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.