Functions

Overview

Please check out VHB course Programming in C++ (Part 1, chapter 2.2) before you start with this chapter. Alternatively, you should be familiar with basics of the C++ type system.

Solution: Type deduction

Problem: How to avoid concrete types for variables to write a more generic code?

As our programs get more complicated, we’ll see that the types we use also get more complicated. Complications in using types arise in two different ways. Some types are hard to “spell.” That is, they have forms that are tedious and error-prone to write.
Moreover, the form of a complicated type can obscure its purpose or meaning. The other source of complication is that sometimes it is hard or even impossible to determine the exact type  we need. The C++ language provides the two keywords "auto" and "decltype" to specify placeholders for concrete types. Those we will introduce in the first lesson along with example use cases.
Placeholder type deduction is the process by which a type containing a placeholder type is replaced by a deduced type.
Bitte bearbeiten Sie den VHB-Kurs Programmierung in C++ (Teil 1, Kapitel 2.2), bevor Sie mit diesem Kapitel beginnen. Alternativ sollten Sie mit den Grundlagen des C++ type system vertraut sein.

Problem: Wie kann man konkrete Typen für Variablen vermeiden, um einen generischeren Code zu schreiben?

Lösung: Type deduction

Wenn unsere Programme komplizierter werden, werden wir sehen, dass die von uns verwendeten Typen auch komplizierter werden. Komplikationen bei der Verwendung von Typen entstehen auf zwei verschiedene Arten. Einige Typen sind schwer zu "buchstabieren". Das heißt, sie haben Formen, die mühsam und fehleranfällig sind. Darüber hinaus kann die Form eines komplizierten Typs seinen Zweck oder seine Bedeutung verdecken.

Die andere Komplikationsquelle ist, dass es manchmal schwierig oder sogar unmöglich ist, den genauen Typ zu bestimmen, den wir brauchen. Die C++-Sprache stellt die beiden Schlüsselwörter "auto" und "decltype" zur Verfügung, um Platzhalter für konkrete Typen anzugeben. Diese werden wir in der ersten Lektion, zusammen mit exemplarischen Anwendungsfällen vorstellen.
Placeholder type deduction ist der Prozess, bei dem ein Typ, der einen Platzhaltertyp enthält, durch einen abgeleiteten Typen ersetzt wird.


No comment has been posted yet.