variants in C++ (variants in C++), Lektion, Seite 724226
https://www.purl.org/stefan_ram/pub/variant_c++ (Permalink) ist die kanonische URI dieser Seite.
Stefan Ram

Varianten in C++ 

Dies ist nur ein kurzer Ausblick, keine ausführliche Lektion.

main.cpp

#include <cstdio>
#include <iostream>
#include <ostream>
#include <string>
#include <variant>

using namespace ::std::literals;

void print( ::std::variant< double, ::std::string >const x )
{ ::std::holds_alternative< double >( x )?
::std::cout << ::std::get< double >( x )<< '\n':
::std::cout << ::std::get< ::std::string >( x )<< '\n'; }

::std::variant< double, ::std::string >reciprocal( double const x )
{ return x?
static_cast< ::std::variant< double, ::std::string >>( 1/x ):
static_cast< ::std::variant< double, ::std::string >>( "undefined"s ); }

int main()
{ print( reciprocal( 2 ));
print( reciprocal( 0 )); }

Protokoll
0.5
undefined

 

Seiteninformationen und Impressum   |   Mitteilungsformular  |   "ram@zedat.fu-berlin.de" (ohne die Anführungszeichen) ist die Netzpostadresse von Stefan Ram.   |   Eine Verbindung zur Stefan-Ram-Startseite befindet sich oben auf dieser Seite hinter dem Text "Stefan Ram".)  |   Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram. Schlüsselwörter zu dieser Seite/relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram724226 stefan_ram:724226 variants in C++ Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd724226, slrprddef724226, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram.
https://www.purl.org/stefan_ram/pub/variant_c++