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
Add backport of std::variant #59
Conversation
virtual const char *what() const noexcept override { return "bad_variant_access"; } | ||
}; | ||
|
||
[[noreturn]] inline void throw_bad_variant_access() |
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 code unconditionally uses [[noreturn]] here, which means it does require C++11 as a minimum. So in the other spot you also don't have to check for codecvt presence, it's just gonna be there... OR you'd need a compiler-version check here OR not use [[noreturn]] attribute at all.
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.
C++11 isn't all or nothing.
Older compilers have support for parts of it.
This code should be portable. It's taken from https://github.com/mpark/variant and tested against a range of older compilers
https://github.com/mpark/variant#requirements
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.
I meant to say that the [[noreturn]] is part of C++11. Same with codecvt . If one place of code (here) unconditionally requires a feature that only officially exists in C++11, then we may as well require the other parts of code to freely use C++11 features too without performing any extra checks..
Otherwise we'd try to support older compilers that may have only partial support of that. Then, why not supporting vs2013, and that'd be a problem for heavily templated code and many expressions (like constexpr, static initializers, etc.). We probably just have to say that we only support compilers that have "good" (near-complete) support of C++11 and above.
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.
gcc 4.8 is still widely used and support most of C++11, but not codecvt. But why worry about this when it's easy to work around.
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.
I like this approach. I'm not exactly sure how to handle homogeneous arrays with variants though.
static_assert(0 < sizeof...(Ts), "variant must consist of at least one alternative."); | ||
|
||
static_assert(detail::all<!std::is_array<Ts>::value...>::value, | ||
"variant can not have an array type as an alternative."); |
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.
According to the spec, attribute values can be homogeneous arrays. I'm not sure how to best handle this with the variant approach.
Also, I expect attribute array values to be a problem for a bunch of vendors, but that's a different story.
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.
Arrays of primitives could be supported with something like
using AttributeValue = nostd::variant<
int,
double,
nostd::string_view,
nostd::span<int>,
nostd::span<double>,
nostd::span<nostd::string_view>
>;
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.
I added support for arrays of primitives. There's an example here
This seems a bit excessive. 22 files and adding a new license. In opencensus-cpp, we have an AttributeValue with 3 constructors and call it a day. |
@g-easy But there are definite advantages to a variant approach
One-off variant approaches like opencensus's AttributeValueRef have a maintenance cost you have to pay any time you want to include an additional type and get especially more complicated if you want to include a type with a non-trivial destructor. |
I'm approving this. My feedback:
I logged a separate issue on this #67 |
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.
Approved with comments above.
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.
Looks good to me. Approved pending the open license question.
Will be used for AttributeValue and various parts of the SDK
Based off of https://github.com/mpark/variant which derives from libc++'s std::variant implementation