-
Notifications
You must be signed in to change notification settings - Fork 1
/
binding.cc
38 lines (33 loc) · 833 Bytes
/
binding.cc
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
// Demonstrate use of unpacking for multi-value return type.
#include <istream>
#include <sstream>
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest/doctest.h"
struct entry
{
std::string name;
int value;
};
// read_entry reads name and value from input stream.
entry read_entry(std::istream& is)
{
std::string name;
int value;
is >> name >> value;
return {name, value}; // Bind variables to return type.
}
TEST_CASE("[binding]")
{
{
std::istringstream is("foo 10");
auto rcv = read_entry(is); // Standard return-by-value.
REQUIRE(rcv.name == "foo");
REQUIRE(rcv.value == 10);
}
{
std::istringstream is("bar 100");
auto [name, value] = read_entry(is); // Unpack.
REQUIRE(name == "bar");
REQUIRE(value == 100);
}
}