boost::any with const value types
I'm finding that if I store a `char*` versus `char const*` in a
boost::any object, that this impacts the behavior of boost::any_cast.
I do not find this documented anywhere.
For example, if I store a const qualified character pointer in a
boost::any object, then I do `boost::any_cast
Hi, are you sure you mean char const* instead of char* const? because for the first it is perfectly correct that the any cast throws: this cast would amount to a const cast (e.g. you could change the underlying data even though the stored char const* would normally prohibit this) Best, Oswin On 2016-07-05 20:31, Robert Dailey wrote:
I'm finding that if I store a `char*` versus `char const*` in a boost::any object, that this impacts the behavior of boost::any_cast. I do not find this documented anywhere.
For example, if I store a const qualified character pointer in a boost::any object, then I do `boost::any_cast
(......)`, it will throw. Changing the type to `char const*` fixes it. Is this by design? I find this makes things more difficult to deal with since I don't always know if the type I'm getting is const or not. Also my implementation does not change depending on constness. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
AMDG On 07/05/2016 12:31 PM, Robert Dailey wrote:
I'm finding that if I store a `char*` versus `char const*` in a boost::any object, that this impacts the behavior of boost::any_cast. I do not find this documented anywhere.
For example, if I store a const qualified character pointer in a boost::any object, then I do `boost::any_cast
(......)`, it will throw. Changing the type to `char const*` fixes it. Is this by design? I find this makes things more difficult to deal with since I don't always know if the type I'm getting is const or not. Also my implementation does not change depending on constness.
Yes, this is by design. The rule is that you can only get out the same type that you put in. char * is not the same type as char const *. In Christ, Steven Watanabe
On 8/07/2016 09:24, Steven Watanabe wrote:
Yes, this is by design. The rule is that you can only get out the same type that you put in. char * is not the same type as char const *.
It would be nice if it followed the implicit const rules, eg. such that if a "char *" is stored it would be possible to retrieve either a "char *" or a "char const *" from it. (This is not currently the case, at least not in 1.55.) The reverse must not be possible though -- if a "char const *" is stored then it must be illegal to retrieve a "char *" as this violates const correctness. Although then I suppose it might raise arguments about implicit conversions as well (eg. storing short and fetching int), which are edging closer to a gray area, and which Boost.Any explicitly disavows.
participants (4)
-
Gavin Lambert
-
Oswin Krause
-
Robert Dailey
-
Steven Watanabe