-
Notifications
You must be signed in to change notification settings - Fork 132
Add std::tuple examples #678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ClausKlein
wants to merge
4
commits into
boost-ext:master
Choose a base branch
from
ClausKlein:feature/add-tuple-example
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
#include <atomic> | ||
#include <boost/ut.hpp> | ||
#include <iostream> | ||
#include <optional> | ||
#include <set> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <tuple> | ||
|
||
void test_optional() { | ||
using namespace boost::ut; | ||
|
||
std::cout << std::boolalpha; | ||
|
||
std::optional<int> opt; | ||
std::cout << opt.has_value() << '\n'; | ||
expect(!opt); | ||
|
||
opt = 43; | ||
// Checks whether *this contains a value. | ||
if (opt) { | ||
std::cout << "value set to " << opt.value() << '\n'; | ||
} else { | ||
std::cout << "value not set\n"; | ||
} | ||
expect(opt.has_value()); | ||
expect(opt == 43); | ||
expect(*opt); | ||
|
||
opt.reset(); | ||
if (opt.has_value()) { | ||
std::cout << "value still set to " << opt.value() << '\n'; | ||
} else { | ||
std::cout << "value no longer set\n"; | ||
} | ||
expect(!opt.has_value()); | ||
expect(!opt); | ||
} | ||
|
||
namespace { | ||
std::optional<std::tuple<int, std::string>> findUserById(int id) { | ||
if (id == 42) { | ||
return std::make_tuple(42, "Alice"); | ||
} else { | ||
return std::nullopt; | ||
} | ||
} | ||
|
||
std::tuple<double, char, std::string> get_student(int id) { | ||
switch (id) { | ||
case 0: | ||
return {3.8, 'A', "Lisa Simpson"}; | ||
case 1: | ||
return {2.9, 'C', "Milhouse Van Houten"}; | ||
case 2: | ||
return {1.7, 'D', "Ralph Wiggum"}; | ||
case 3: | ||
return {0.6, 'F', "Bart Simpson"}; | ||
} | ||
|
||
throw std::invalid_argument("id"); | ||
} | ||
|
||
struct S { | ||
int n; | ||
std::string s; | ||
double d; | ||
|
||
friend bool operator<(const S& lhs, const S& rhs) noexcept { | ||
// compares lhs.n to rhs.n, | ||
// then lhs.s to rhs.s, | ||
// then lhs.d to rhs.d | ||
// in that order, first non-equal result is returned | ||
// or false if all elements are equal | ||
return std::tie(lhs.n, lhs.s, lhs.d) < std::tie(rhs.n, rhs.s, rhs.d); | ||
} | ||
}; | ||
|
||
std::tuple<int, int> divide(int a, int b) { | ||
return std::make_tuple(a / b, a % b); // quotient and remainder | ||
} | ||
|
||
} // namespace | ||
|
||
int main() { | ||
using namespace boost::ut; | ||
|
||
const auto student0 = get_student(0); | ||
std::cout << "ID: 0, " | ||
<< "GPA: " << std::get<0>(student0) << ", " | ||
<< "grade: " << std::get<1>(student0) << ", " | ||
<< "name: " << std::get<2>(student0) << '\n'; | ||
expect(std::tuple_size<decltype(student0)>::value == 3); | ||
expect(std::get<2>(student0) == "Lisa Simpson"); | ||
|
||
const auto student1 = get_student(1); | ||
std::cout << "ID: 1, " | ||
<< "GPA: " << std::get<double>(student1) << ", " | ||
<< "grade: " << std::get<char>(student1) << ", " | ||
<< "name: " << std::get<std::string>(student1) << '\n'; | ||
expect(std::get<2>(student1) == "Milhouse Van Houten"); | ||
|
||
double gpa2{}; | ||
char grade2{}; | ||
std::string name2; | ||
std::tie(gpa2, grade2, name2) = get_student(2); | ||
std::cout << "ID: 2, " | ||
<< "GPA: " << gpa2 << ", " | ||
<< "grade: " << grade2 << ", " | ||
<< "name: " << name2 << '\n'; | ||
expect(get_student(2) == | ||
std::tuple<double, char, std::string>{1.7, 'D', "Ralph Wiggum"}); | ||
|
||
// C++17 structured binding: | ||
const auto [gpa3, grade3, name3] = get_student(3); | ||
std::cout << "ID: 3, " | ||
<< "GPA: " << gpa3 << ", " | ||
<< "grade: " << grade3 << ", " | ||
<< "name: " << name3 << '\n'; | ||
expect(get_student(3) == | ||
std::tuple<double, char, std::string>{0.6, 'F', "Bart Simpson"}); | ||
|
||
// Lexicographical comparison demo: | ||
std::set<S> set_of_s; | ||
|
||
S value{42, "Test", 3.14}; | ||
std::set<S>::iterator iter; | ||
bool is_inserted{}; | ||
|
||
// Unpack a pair: | ||
std::tie(iter, is_inserted) = set_of_s.insert(value); | ||
expect(is_inserted); | ||
|
||
// std::tie and structured bindings: | ||
auto position = [](int w) { return std::tuple(1 * w, 2 * w); }; | ||
|
||
auto [x, y] = position(1); | ||
expect(x == 1 && y == 2); | ||
std::tie(x, y) = position(2); // reuse x, y with tie | ||
expect(x == 2 && y == 4); | ||
|
||
// Prevent warning C4244: 'initializing': conversion from '_Ty' to '_Ty', possible loss of data | ||
// Implicit conversions are permitted: | ||
// std::tuple<char, short> coordinates(6, 9); | ||
// std::tie(x, y) = coordinates; | ||
// expect(x == 6 && y == 9); | ||
|
||
// Skip an element: | ||
std::string z; | ||
std::tie(x, std::ignore, z) = std::tuple(1, 2.0, "Test"); | ||
expect(x == 1 && z == "Test"); | ||
|
||
// Multiple Return Values | ||
auto [quotient, remainder] = divide(10, 3); | ||
expect(quotient == 3); | ||
expect(remainder == 1); | ||
|
||
expect(std::tuple(2, 1) == divide(5, 2)); | ||
|
||
std::atomic<bool> test_value{false}; | ||
expect(!test_value); | ||
|
||
// test std::optional<std::tuple<>> | ||
{ | ||
auto result = findUserById(42); | ||
//FIXME: expect(result); | ||
if (result) { | ||
// To unpack, use *result or result.value(). | ||
auto [userId, username] = *result; // unpack the tuple | ||
std::cout << "Found user: " << userId << ", " << username << "\n"; | ||
} else { | ||
std::cout << "User not found.\n"; | ||
} | ||
expect(result != std::nullopt); | ||
} | ||
|
||
test_optional(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not compile?