templates and namespaces
play

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


  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

  2. Outline 1. Namespaces Introduction Namespaces in your project 2. Templates Function templates Class templates Advanced usage Pros and cons 2

  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

  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

  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

  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 // same as foo::func(x) 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

  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

  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 foo/bar/bar.h namespace foo { namespace foo { void TopLevelFunction( int x); namespace bar { } void SubPackageFunction( int x, int y); } main.cpp void AnotherTopLevelFunction( int x); } #include <foo/foo.h> #include <foo/bar/bar.h> int main() { int x = 5, y = 10; foo::TopLevelFunction(x); foo::bar::SubPackageFunction(x, y); } 8

  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

  10. Function template example • Provide the function template < typename T> T GetMax(T a, T b); template with a type { • or let the compiler infer T result = a > b ? a : b; the type implicitly return result; } • The types must match 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! } 10

  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

  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

  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

  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

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend