Templates and Namespaces Visual Computing in OpenCV: Seminar 1 - - PowerPoint PPT Presentation

templates and namespaces
SMART_READER_LITE
LIVE PREVIEW

Templates and Namespaces Visual Computing in OpenCV: Seminar 1 - - PowerPoint PPT Presentation

Templates and Namespaces Visual Computing in OpenCV: Seminar 1 Hannes Ovrn Department of Electrical Engineering Computer Vision Laboratory Linkping University August 23, 2012 Outline 1. Namespaces Introduction Namespaces in your


slide-1
SLIDE 1

Templates and Namespaces

Visual Computing in OpenCV: Seminar 1 Hannes Ovrén

Department of Electrical Engineering Computer Vision Laboratory Linköping University August 23, 2012

slide-2
SLIDE 2

Outline

  • 1. Namespaces

Introduction Namespaces in your project

  • 2. Templates

Function templates Class templates Advanced usage Pros and cons

2

slide-3
SLIDE 3

Namespaces

Naming collisions can be a problem when using external code

foo/foo.h

int var = 10;

mysrc.cpp

#include <foo/foo.h> int var = 5; // Compile error! int main() { std::cout << var << std::endl; }

3

slide-4
SLIDE 4

Namespaces to the rescue

Using namespaces, the problem of naming collisions is removed

foo/foo.h

namespace foo { int var = 10; }

mysrc.cpp

#include <foo/foo.h> int var = 5; // Look ma! No collision! int main() { std::cout << var << std::endl; // Prints ’5’ std::cout << foo::var << std::endl; // Prints ’10’ }

4

slide-5
SLIDE 5

Using namespaces

You declare namespaces with the namespace keyword.

namespace foo { int var; void func(int x); }

To call the code you must use the namespace identifier “::”.

#include <foo/foo.h> int main() { int x = foo::var; foo::func(x); return 0; }

5

slide-6
SLIDE 6

The “using” keyword

If you get tired of writing the namespace identifier you can use the “using” keyword.

#include <foo/foo.h> using namespace foo; int main() { int x = var; // same as x = foo::var func(x); // same as foo::func(x) return 0; }

Use with care as it defeats the purpose of namespacing.

#include <foo/foo.h> using namespace foo; int main() { int var = 7; // Oops! This hides foo::var! return 0; }

6

slide-7
SLIDE 7

Nested namespaces and shortcuts

Namespaces can be nested

namespace foo { namespace bar { void func(int x); } } int main() { int x = 5; foo::bar::func(x); }

Spare your keyboard by aliasing a long namespace

namespace fb = foo::bar; // Alias fb::func(x); // Same as foo::bar::func(x)

7

slide-8
SLIDE 8

Namespaces in your project

  • Aim to use a namespace for all your non-trivial projects
  • Always use a namespace if you are writing a library that are used by

multiple projects.

  • Be careful with "using namespace"
  • Namespaces can be used in multiple files

foo/foo.h

namespace foo { void TopLevelFunction(int x); }

main.cpp

#include <foo/foo.h> #include <foo/bar/bar.h> int main() { int x = 5, y = 10; foo::TopLevelFunction(x); foo::bar::SubPackageFunction(x, y); }

foo/bar/bar.h

namespace foo { namespace bar { void SubPackageFunction(int x, int y); } void AnotherTopLevelFunction(int x); }

8

slide-9
SLIDE 9

Templates

  • Template functions and template classes
  • Write generic code to handle multiple types
  • Generates new code from template at compile time

Function Template Syntax

Both versions are equivalent (I prefer typename):

template <typename T> function_declaration; template <class T> function_declaration;

The T parameter is just a type name and can be named anything.

Example

template <typename T> T GetMax(T a, T b);

9

slide-10
SLIDE 10

Function template example

template <typename T> T GetMax(T a, T b); { T result = a > b ? a : b; return result; } int main() { int i=5, j=10; long l=15, m=45; i = GetMax<int>(i, j); // Explicit type i = GetMax(i, j); // Implicit type l = GetMax<long>(l, m); // Explicit type l = GetMax(i, m); // ERROR: both parameters must // have the same type! }

  • Provide the function

template with a type

  • or let the compiler infer

the type implicitly

  • The types must match

10

slide-11
SLIDE 11

Multiple types

Let’s modify the GetMax example to handle two different types!

template <typename FirstT, typename SecondT> FirstT GetMax(FirstT a, SecondT b); { FirstT result = a > b ? a : b; return result; } int main() { int i = 10; long m = 20; int j; long l; j = GetMax<int, long>(i, m); // Returns a int l = GetMax<long, int>(m, i); // Returns a long }

11

slide-12
SLIDE 12

Class templates

Works pretty much the same as function templates. Here is an example of a generic tuple class that holds a pair of items of any type.

template <typename T> class MyTuple { public: MyPoint(T first, T second) { m_first = first; m_second = second; } T getFirst(void) { return m_first; } // Full definition T getSecond(void); // Only declaration private: T m_first; T m_second; } template <typename T> T MyTuple<T>::getSecond(void) // Definition { return m_second; }

12

slide-13
SLIDE 13

Non-type template parameters

Example: Class that contains a configurable amount of some item type.

template <typename ItemT, int N> class Container { private: ItemT m_data[N]; public: bool setItem(int index, ItemT item) { if ((index >= N) || (index < 0)) return false; // Index out of bounds m_data[index] = item; return true; } } Container<char, 5> myContainer; // Holds 5 chars myContainer.setItem(3, ’a’); // returns true myContainer.setItem(10, ’b’); // returns false (10 >= 5)

Note that the size is determined at compile time and can not be changed during execution!

13

slide-14
SLIDE 14

Specialized templates

If some specific type requires or benefits from a specific implementation, you can specify the implementation yourself.

// Generic template implementation template <typename T> T GetMax(T a, T b) { return a >= b ? a : b; } // For chars, ignore case template <> // empty type char GetMax<char>(char a, char b) // but note the <char> here! { // Convert to lowercase char a_low = tolower(a); char b_low = tolower(b); return a_low >= b_low ? a : b; } // Examples char a = ’a’; // value: 97 char b = ’B’; // value: 66 char c; c = a >= b ? a : b; // returns ’a’ (mimic generic GetMax) c = GetMax<char>(a, b); // returns ’B’

14

slide-15
SLIDE 15

Templates pros and cons

Pros

  • Write generic algorithms and container types
  • Compile time checked

Cons and caveats

  • Increased compile time
  • (Risk of) larger executable
  • Makes debugging harder
  • Templates must be in header files. Change triggers project wide rebuild.
  • Definition must be in same file as declaration. Impossible to separate

interface and implementation.

15

slide-16
SLIDE 16