Coder Social home page Coder Social logo

optional's Introduction

C++ Optional

Purpose

A single-header header-only library for representing optional (nullable) objects for C++

TEST 1

#include "optional.hxx"
#include <iostream>

int main()
{
    auto opt = make_optional<int>(124.52f);

    std::cout << std::boolalpha << "bool(opt): " << bool(opt) << std::endl; 
    std::cout << "*opt: " << *opt << std::endl;


    if ( auto obj = optional<int>(nullopt) ) {
        std::cout << "success!" << std::endl;
    }
    else {
        std::cout << "failed!" << std::endl;
    }

}

TEST 2

#include "optional.hxx"
#include <format>
#include <optional>

class base
{
public: 
    constexpr base(int a, int b) noexcept(true) : m_a(a), m_b(b) {}

    constexpr virtual ~base() noexcept(true) = default;

    virtual void print() const noexcept(true)
    {
        std::cout << std::format("base( a: {}, b: {} )\n", m_a, m_b);
    }
protected:
    int m_a;
    int m_b;
};

class child final: public base
{
public:
    constexpr child(float f, int a, int b) noexcept(true) : base(a,b), m_f(f) {}

    void print() const noexcept(true)
    {
        std::cout << std::format("child( f: {:+.2f}, a: {}, b: {} )\n", m_f, m_a, m_b);
    }

public:
    constexpr ~child() noexcept(true) = default;

private:
    float m_f;
};


int main()
{
    auto child_ = child(1.25f, 58, 96);
    auto base_  = base(66,34);
    optional<base&> obj1(child_), obj2(base_);

    obj1->print(); // child( f: +1.25, a: 58, b: 96 )
    obj2->print(); // base( a: 66, b: 34 )
}

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.