(C++) TemplateFunctionExample1
February 24, 2017 · View on GitHub
(C++) TemplateFunctionExample1
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
STL: GNU ISO C++ Library, version
4.9.2
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); }