-
Notifications
You must be signed in to change notification settings - Fork 85
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
Explicit underlying cast #6
base: master
Are you sure you want to change the base?
Explicit underlying cast #6
Conversation
# Conflicts: # named_type.hpp
Guillaume, Thanks a lot for contributing to NamedType. I am sorry for the late reply, I didn't see the PR earlier. |
Hi Jonathan, |
Guillaume, would the ImplicitlyConvertibleTo skill help more in your case then? |
ImplicitlyConvertibleTo would defeat my purpose for using NamedType as I want a cast to be explicit. |
No major downside, it only gives more access to the underlying type, and the STL doesn't do it with unique_ptr. But I understand the need. Would it make sense for you if it was implemented as a skill "ExplicitlyConvertible"? |
Yes, it would make good sense to have it as a skill! I need to find sometime to rework it then. |
+1 on having an |
In fact, I would argue that making all One missing piece is a protocol for querying the underlying type, something like I suppose generic code could be made to support the direct member |
Arvid, thanks for your suggestion. Are you thinking about particular examples that use |
I think the most obvious example is for enums. I would imagine that generic code use The other major example I have in mind is boost.endian. I'm using this to build network packets, to abstract alignment and endian requirements. Currently, boost.endian doesn't really have support for strong types, but only supports raw integers. However, I would greatly improve its type safety if the value_type used in boost.endian could be something more restricted than a raw integer. But since boost.endian is responsible for the actual binary representation of the value, it needs to deal with raw integer types, that it can shift, AND and OR. In a way, it deals with the storage of values, and the mapping of the storage to values. In order for boost.endian to support arbitrary value types, it would need to have a protocol to:
I'm interested in proposing a patch to boost.endian to support this. |
An alternative is to build on top of Boost.Endian an endian type that can take a UDT types instead of an integral builtin type. |
Yes, the standard conversion protocol uses static_cast. I believe both skills (implicit and explicit conversion to) are useful and must not be hard coded in the strong type base type (NamedType in this case). However, in order to get the underlying type I believe that conversion are cumbersome. We need something else. A BTW, I believe that |
@viboes Yes, once I actually started working on it, with the intention to patch boost.endian, it turned out to be much simpler to build a new primitive on top of boost.endian that glues together both NamedType, enums and some other types that I had (that already supported a tag template parameter for unique types) |
Maybe it is worth understanding why |
In my project I need to:
I thought that need is general enough that it would be useful to all NamedType users and that it's not taking away any existing feature, hence that PR.