-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp14_tuple.cpp
More file actions
51 lines (39 loc) · 1.28 KB
/
cpp14_tuple.cpp
File metadata and controls
51 lines (39 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <tuple> // Required for std::tuple
#include <string>
using MyTuple = std::tuple<int, double, std::string>;
/*
* Returning multiple values from a function by binding them in a single
* tuple object.
*/
MyTuple GetMyTuple()
{
// Creating a tuple of int, double and string
MyTuple result(7, 9.8, "text");
// Returning tuple object
return result;
}
int main()
{
// Get tuple object from a function
MyTuple result = GetMyTuple();
// Get values from tuple
int iVal = std::get<0>(result);
double dVal = std::get<1>(result);
std::string strVal = std::get<2>(result);
// Print values
std::cout << "int value = " << iVal << std::endl;
std::cout << "double value = " << dVal << std::endl;
std::cout << "string value = " << strVal << std::endl;
// Get 4th int value from tuple Will cause compile error
// because this tuple has only 3 elements
// int iVal2 = std::get<4>(result); // Compile error
// Wrong cast will force compile time error
// std::string strVal2 = std::get<0>(result); // Compile error
int i = 1;
const int const_i = 1;
// Compile error because i is not compile time contant
// double dVal2 = std::get<i>(result);
double dVal3 = std::get<const_i>(result);
return 0;
}