Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C++

Template Meta Programming and Number Theory: Part 2

Rate me:
Please Sign up or sign in to vote.
4.67/5 (13 votes)
24 Aug 2007CPOL7 min read 45.8K   205   24  
Using C++ language constructs to introduce abstraction layers
// Functional.h
// Author: Zeeshan Amjad
// 
// Some Basic function for template meta programming
//

#ifndef _FUNCTIONAL_H
#define _FUNCTIONAL_H

// return the value of first parameter
template <int u, int v>
struct Value
{
	enum { value = u };
};

// add two numbers. 
// If second value is missing then return the first value 
template <int u, int v = 0>
struct Add
{
	enum { value = u + v };
};

// subtract one number from another
// If second value is missing then return the first value 
template <int u, int v = 0>
struct Subtract
{
	enum { value = u - v };
};

// subtract one number from another
// If second value is missing then return the first value 
template <int u, int v = 1>
struct Multiply
{
	enum { value = u * v };
};

// divide one number by another
// If second value is missing then return the first value 
template <int u, int v = 1>
struct Divide
{
	enum { value = u / v };
};

// Modulo operator
template <int u, int v>
struct Mod
{
	enum { value = u % v };
};

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader American Institute for Research
United States United States
Working as a Team leader in American Institute for Research

Comments and Discussions