C++17: return parameters in functions
p_806.cpp
Go to the documentation of this file.
1 // Bsp. Celsius nach Fahrenheit und Kelvin.
2 // Alternative Outputuebergabe nach:
3 // T. Will: C++ Das umfassende Handbuch, p.806ff (ยง28.1.1); ; Rheinwerk Verlag, 2018
4 
5 // g++ -std=c++14 -Wfatal-errors -Wall -Wextra -pedantic p_806.cpp (without lines 47-49)
6 // g++ -std=c++17 -Wfatal-errors -Wall -Wextra -pedantic p_806.cpp (gcc-7.2)
7 // clang++ -std=c++17 -Wc++17-extensions p_806.cpp
8 
9 
10 #include <iostream>
11 #include <utility> // pair
12 #include <tuple> // tuple
13 
14 //using std:cout; using std:endl;
15 //using std::pair;
16 using namespace std;
17 
18 constexpr
19 pair<float, float> c2fk(const float celsius)
20 {
21  return make_pair( celsius*9.0f/5+32.0f, celsius+273.15f);
22 }
23 
24 constexpr
25 tuple<float, float> c2fk_tup(const float celsius)
26 {
27  return make_tuple( celsius*9.0f/5+32.0f, celsius+273.15f);
28 }
29 
30 
31 int main()
32 {
33  float c{};
34  cout << " Celsius : "; cin >> c;
35 
36  const auto pp = c2fk(c);
37  cout << " Fahrenheit : " << pp.first << endl;
38  cout << " Kelvin : " << pp.second << endl;
39  cout << "--------------------------- " << endl;
40 
41  const auto tt = c2fk_tup(c);
42  cout << " Fahrenheit : " << get<0>(tt) << endl;
43  cout << " Kelvin : " << get<1>(tt) << endl;
44  cout << "--------------------------- " << endl;
45 
46  float f{}, k{};
47  tie(f,k) = c2fk_tup(c);
48  cout << " Fahrenheit : " << f << endl;
49  cout << " Kelvin : " << k << endl;
50  cout << "--------------------------- " << endl;
51 
52 // https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
53 //#if __GNUC__ >= 7 && _GNUC_MINOR__ >= 2
54  auto [ff,kk] = c2fk_tup(c); // : -std=c++1z (gcc-7.2)
55  cout << "-- C++17 --" << endl;
56  cout << " Fahrenheit : " << ff << endl;
57  cout << " Kelvin : " << kk << endl;
58  cout << "--------------------------- " << endl;
59 //#endif
60 
61  return 0;
62 }
63 
constexpr pair< float, float > c2fk(const float celsius)
Definition: p_806.cpp:19
constexpr tuple< float, float > c2fk_tup(const float celsius)
Definition: p_806.cpp:25
int main()
Definition: p_806.cpp:31