Templates & STL Stefan Roiser, Lorenzo Moneta CERN PH/SFT.

Slides:



Advertisements
Similar presentations
Yaser Zhian Dead Mage IGDI, Workshop 10, May 30 th -31 st, 2013.
Advertisements

SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
INTEL CONFIDENTIAL Threading for Performance with Intel® Threading Building Blocks Session:
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Vectors, lists and queues
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Introduction to the STL
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
Generic Programming David Rabinowitz. June 14, 2006 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack.
1 Lecture 8: Introduction to C++ Templates and Exceptions  C++ Function Templates  C++ Class Templates.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Templates and the STL.
CSE 332: Combining STL features Combining STL Features STL has containers, iterators, algorithms, and functors –With several to many different varieties.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.
Data Structures Using C++ 2E
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
GPCE'04, Vancouver 1 Towards a General Template Introspection Library in C++ István Zólyomi, Zoltán Porkoláb Department of Programming Languages and Compilers.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
CS 213 Fall 1998 Namespaces Inline functions Preprocessing Compiling Name mangling Linking.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
CSE 332: C++ template examples Concepts and Models Templates impose requirements on type parameters –Types that are plugged in must meet those requirements.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
Overview of C++ Templates
1 Chapter 1 C++ Templates Sections 1.6 and Templates Type-independent patterns that can work with multiple data types –Generic programming –Code.
Copyright 2006, The Ohio State University Introduction to C++ Templates l C++ Function Templates l C++ Class Templates.
Chapter 3 Templates Saurav Karmakar Spring Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates.
Feedback from LHC Experiments on using CLHEP Lorenzo Moneta CLHEP workshop 28 January 2003.
Template is a declaration (similar to class declaration), which deals with generic types. Templates are evaluated to produce concrete classes when a template-base.
Concepts in C++. Templates in current C++ C++ template is typeless No language support for constrained generics Accidental errors found in instantiation.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Templates Consider the following function, which swaps two integers: void swap(int &x, int &y) { int temp = x; x = y; y = temp; } int i = 3, j = 4; swap(i,
2/26/ Customization Points and Polymorphism Functional Programming Boost.Lambda.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (this Friday). Today: –Templates. –STL –Smart Pointers.
C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
Templates 3 Templates and type parameters The basic idea templates is simple: we can make code depend on parameters, so that it can be used in different.
Duke CPS Iterators: Patterns and STL l Access a container without knowing how it’s implemented ä libtapestry: first, isDone, next, current iterators.
C++ REVIEW – TEMPLATES. GENERIC PROGRAMMING Programming/developing algorithms with the abstraction of types Algorithms/data is expressed “without type”
CPSC 252 Templatization Page 1 Templatization In CPSC152, we saw a class vector in which we could specify the type of values that are stored: vector intData(
Object-Oriented Programming (OOP) Lecture No. 41
C++ Lesson 1.
How to be generic Lecture 10
C++ Templates.
Templates.
Standard Template Library (STL)
STACKS AND QUEUES UNIT 2 DS THROUGH C++.
Templates.
ADT Implementations: Templates and Standard Containers
C++ Functions, Classes, and Templates
Templates. Templates Example: Swap Template Mechanism.
CSE 303 Lecture 25 Loose Ends and Random Topics
Tenth step for Learning C++ Programming
Presentation transcript:

Templates & STL Stefan Roiser, Lorenzo Moneta CERN PH/SFT

Current Usage Template types are highly used in HEP –Via STL –Libraries provided by external parties E.g. Boost –Libraries provided by CERN developers E.g. SMatrix, a library for linear algebra –Uses expression templates Templates & STL

Symbols Generic vector ? Problems –Length of symbols Source code type typedef std::map > MyMap; Mangled symbol (constructor) __ZNSt3mapISsSt4pairIiiESt4lessISsESaIS0_IKSsS1_EEEC1Ev Demangled symbol std::map, std::allocator >, std::pair, std::less, std:: allocator > >, std::allocator, std::allocator > const, std::pair > > >::map() –Number of symbols Symbols for member functions of class for every instantiation Results in –Code bloat Many of these symbols are not even used –Increased library sizes and load time E.g. literal type names are stored in reflection libraries –Lengthy compiler error messages Templates & STL

Concepts Narrowing template types a la “concepts” ROOT::Math::LorentzVector > float or double Must provide functions x(), y(), z() Currently a wrong instantiation produces LorentzVector.h: In member function 'typename CoordSystem::Scalar LorentzVector ::x() const [with CoordSystem = Foo]': testMessage.cxx:13: instantiated from here LorentzVector.h:616: error: 'const struct Foo' has no member named 'Px' Templates & STL

Other Issues Thread safety for STL? –E.g. “Intel Threading Building Blocks” Provides implementations for hash_map, queue, vector Enhancements for iterator programming –Simplifications for the coding itself E.g. using lambda functions Templates & STL