Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) TemplateFunctionExample1

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppTemplateFunctionExample1/CppTemplateFunctionExample1.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

CONFIG(release, debug|release) {
  DEFINES += NDEBUG
}

QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++

unix {
  QMAKE_CXXFLAGS += -Werror
}

win32 {
  INCLUDEPATH += \
    ../../Libraries/boost_1_54_0
}

 

 

 

 

 

./CppTemplateFunctionExample1/main.cpp

 

int AbsInt(const int n) { return n < 0 ? -n : n; }

double AbsDouble(const double n) { return (n < 0 ? -n : n); }

template <class T>
T Abs(const T n) { return n < 0 ? -n : n; }

#include <cassert>
#include <cmath>

int main()
{
  const double pi = M_PI;

  assert(AbsInt( 42) == 42);
  assert(AbsInt(-42) == 42);
  assert(AbsDouble( pi) == pi);
  assert(AbsDouble(-pi) == pi);

  assert(Abs( 42) == 42);
  assert(Abs(-42) == 42);
  assert(Abs( pi) == pi);
  assert(Abs(-pi) == pi);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml