Skip to content
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

Extend cxxopts API to support std::optional non-throwing query of values #421

Merged
merged 1 commit into from
Feb 29, 2024

Conversation

nigels-com
Copy link
Contributor

@nigels-com nigels-com commented Feb 23, 2024

We have some internal usage of std::optional that can integrate very tidily with cxxopts providing this query for std::optional value of given name and type.

No need for an if-check of count or catching an exception.


This change is Reviewable

@jarro2783
Copy link
Owner

Thanks for your PR. I will take a look properly when I get a chance in a couple of days.

@nigels-com
Copy link
Contributor Author

No rush. We're having some interesting internal discussion.
Want to check a few assumptions when I get a chance.

include/cxxopts.hpp Outdated Show resolved Hide resolved
@jarro2783
Copy link
Owner

This looks good, I'm also thinking about whether there is a better name for the function. I wonder if there some convention from other projects like something with maybe in the name.

@jarro2783
Copy link
Owner

What do you think about calling it maybe_as, I think that fits the convention when a function can return an optional, and makes it clear that it is the same behaviour as as.

@nigels-com
Copy link
Contributor Author

I was thinking of make_optional in the manner of std::make_shared.

It turns out that std::make_optional does already exist too. (News to me!)

@nigels-com
Copy link
Contributor Author

I understand your reasoning with maybe but isn't that just a synonym for optional?
Is clarity improved by assigning a "maybe" to an "optional"?

@jarro2783
Copy link
Owner

Or as_optional might be even clearer. I'm just thinking that optional by itself is not the clearest name as to what it does on that class.

@nigels-com
Copy link
Contributor Author

Yes, as_optional sounds good to me.

@nigels-com
Copy link
Contributor Author

Updated as discussed.

@jarro2783 jarro2783 merged commit 70fb4e4 into jarro2783:master Feb 29, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants