Hi,
can anyone explain why an any_cast that adds a low-level const throws??
Example:
char str[] = "a2";
boost::any ay(str);
boost::any_cast
On Wed, Sep 23, 2015 at 9:55 PM, nice sw123 wrote:
Hi,
can anyone explain why an any_cast that adds a low-level const throws??
Example: char str[] = "a2"; boost::any ay(str); boost::any_cast
(ay); // throws, since a const is added!!! The only way to do this (without throwing), seems to be: boost::any_cast
(ay); But why this mild "design deficiency"??
If I change this code
https://github.com/boostorg/any/blob/ed7cac4ee207b125aa25144f2674f9550def223...
to
template<typename ValueType>
ValueType * any_cast(any * operand) BOOST_NOEXCEPT
{
return &static_cast
I was just trying some stuff here.. #include <typeinfo> #include <cassert> using T1 = const char *; using T2 = char *; assert(typeid(T1) != typeid(T2)); // assert(typeid(const T1) == typeid(const T2)); // -> damn!! seems: cannot add low-level const here But look at this: ===== template <typename T> void func(T t) { std::cout << "nonpointer" << std::endl; } template <typename T> void func(T *t) { std::cout << "pointer" << std::endl; } template <typename T> void func(const T *t) { std::cout << "pointer to const" << std::endl; } char str[] = "a1"; func("asdf"); //pointer to const func(str); // pointer func(1); // nonpointer What this tells me is: if it's not possible to add a low-level const to a typeinfo or typeindex, then we could still change the constructor of boost::any, to differentiate between types. Any comments, hints or pointers welcome. (You may have noticed... template and meta is not my strength yet... but I hope to learn)
participants (1)
-
nice sw123