-
Notifications
You must be signed in to change notification settings - Fork 0
Add 14 #9
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
Merged
Add 14 #9
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| cmake_minimum_required(VERSION 3.16.3) | ||
|
|
||
| project(14_optional) | ||
|
|
||
| add_compile_options(-std=c++17) | ||
| add_executable(main main.cpp) |
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,10 @@ | ||
| # 14_optional | ||
|
|
||
| 変数に値が格納されているかどうかを調べることができるクラス | ||
|
|
||
| ROSなどのコールバック関数で値が格納されているかどうかを確認する際にも利用できる | ||
|
|
||
| C++17以降で利用可能なので注意すること | ||
|
|
||
| 参考: | ||
| - https://cpprefjp.github.io/reference/optional/optional.html | ||
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,42 @@ | ||
| // Copyright 2024 AMSL | ||
|
|
||
| #include <iostream> | ||
| #include <optional> | ||
|
|
||
| int main(int argc, char **argv) | ||
| { | ||
| std::optional<int> num; | ||
| if (!num.has_value()) | ||
| { | ||
| std::cout << "値が含まれていません。" << std::endl; | ||
| } | ||
| // 値が含まれていないので値の取得もできない | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 値が無い(無効である)ことを示せるのはそうですが、例えばint型を返す関数で、「返り値が0未満ならエラーを表している」という実装をする代わりに、「エラーの場合は無効値(std::nullopt)を返す。値が入っていればそれは有効である」という実装ができ、その関数を呼び出す側からすると値ではなく型で有効/無効の判別ができる、というのがoptionalの代表的な利点であると思うので、そういうケースについても示せると良いと思います |
||
| try | ||
| { | ||
| std::cout << num.value() << std::endl; | ||
| } | ||
| catch (std::bad_optional_access &e) | ||
| { | ||
| std::cout << "例外: " << e.what() << std::endl; | ||
| } | ||
|
|
||
| num = 1; | ||
| if (num.has_value()) | ||
| { | ||
| std::cout << "値が含まれています。値: " << num.value() << std::endl; | ||
| } | ||
|
|
||
| num.reset(); | ||
| if (!num.has_value()) | ||
| { | ||
| std::cout << "値が含まれていません。" << std::endl; | ||
| } | ||
|
|
||
| // std::optionalではstd::nulloptを無効値として扱う | ||
| num = std::nullopt; // std::nulloptを使ってリセット | ||
| if (!num.has_value()) | ||
| { | ||
| std::cout << "有効値が含まれていません。" << std::endl; | ||
| } | ||
| return 0; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
どういうユースケースなのかよくわからないです。ROSのサブスクライバのコールバック関数は値をポインタで渡してくるので、単にそれがnullptrかどうかを確認すれば良いと思います。また、コールバック関数が呼ばれたときに引数がnullptrでない保証があるかどうかは把握していないですが、普通わざわざ確認しないと思います