Giter Club home page Giter Club logo

cpp07's Introduction

CPP07

Templates

template <typename T>
T const & max(T const &x, T const &y)
{
    return (x >= y ? x : y);
}

int foo(int x)
{
    return (x);
}

int main()
{
    int a = 21;
    int b = 42;

    std::cout << max<int>(a, b) << "\n"; // Explicit instanciation
    std::cout << max(a, b) << "\n"; // Implicit instanciation

    float c = -21.1f;
    float d = 4.2f;

    std::cout << max<float>(c, d) << "\n"; // Explicit instanciation
    std::cout << max(c, d) << "\n"; // Implicit instanciation

    char e = 'e';
    char f = 'f';

    std::cout << max<char>(e,f) << "\n"; // Explicit instanciation
    std::cout << max(e,f) << "\n"; // Implicit instanciation

    int ret = max<int>(foo(a), foo(b)); // Explicit instanciation
    std::cout << ret << "\n";
}

Default Types

template <typename T = float> // Default type is float
class Vertex
{
    public:
    Vertex(T const &x, T const &y, T const &z) : _x(x), _y(y), _z(z)
    
    // Etc..
};

// << operator overload

template <typename T>
std::ostream & operator<<(std::ostream& o, Vertex<T> const &v)
{
    std::cout << v.getX() << v.getY() << v.getZ();
    return o;
}

Specialization

// Generic template
template <typename T, typename U>
class Pair
{
    Pair<T, U>(T const &lhs, U const &rhs) : _lhs(lhs), _rhs(rhs)
    {
        // Etc
    }
    T const& fst(void) const {return this->_lhs;}
    T const& snd(void) const {return this->_rhs;}
}

// Int partial specialization template
template <typename U>
class Pair<int, U> //
{
    Pair<int, U>(int const &lhs, U const &rhs) : _lhs(lhs), _rhs(rhs)
    {
        // Etc
    }
}

// bool/bool specialization
template <>
class Pair<bool, bool>
{
    Pair<bool, bool>(bool const &lhs, bool const &rhs)
    {
        // Etc
    }
}

template<typename T, typename U>
std::ostream& operator<<(std::ostream& o, Pair<T, U> const& p)
{
    o << p.fst() << p.scd() << "\n";
    return (o);
}

// Specialized ostream
std::ostream& operator<<(std::ostream& o, Pair<bool, bool> const& p)
{
    o << p.fst() << p.scd() << "\n";
    return (o);
}

cpp07's People

Watchers

Pedro Santos 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.