# Any Struggles

In C++ you can’t just forget about types. Each variable has its own type that is not going to be changed by any means. What if you really need something heterogeneous? There is a known idiom called Any that enables you to erase the type and recall it later.

Let’s consider the following example:

Doesn’t it look useful? Unfortunately, there is no notion of Any neither in the C++11 nor the C++14 standard library. Although there is no reason to not use it (and it is widely used) so let’s consider two available non-standard implementations: the first one is Boost.Any and the other one is Any by Christopher Diggins.

If you glance over the code of these implementations you would definitely notice that both implementations follow some similar pattern that involves templated descendant of some non-templated base class (this implements type erasure). This resolves two major issues:

• How to make cast from any to some T safe – i.e. how to check if the cast type is correct. Being able to access effective type the templated class can definitely provide all the information for such checks.

• How to copy, move and access the value. The templated class has the type in its scope so this is quite easy.

Nevertheless, they are a bit different in details. Let’s consider the following snippet:

This illustrates the approach of Boost.Any. You may notice that the implementation always involves typeid (a part of RTTI) for safe casts, which could affect performance. Can we get any better? Actually yes: Christopher Diggins has made an improvement over this approach. His implementation of Any can be described with the following structure of classes:

Essentially, the idea is to keep all used policies online as static objects so we can easily compare them. Policy for any type T stays the same during the runtime (up to pointer) so it is quite easy to check whether the user tries to cast to the correct type.

### Performance

The claim that really needs some support is: boost::any is not that fast as cdiggins::any. Obviously, typeid should affect performance but it is totally unclear how significant the difference is.

I didn’t pretend to do any thorough research on that – so just a simple benchmark made with Hayai looks like that:

What I got on my machine (i5, Ubuntu 14.04, GCC 4.8.2 and -std=c++11 -O3) is:

This doesn’t look significant but still we now know the difference ;) I shared the benchmark code on github so please try it on your own!

### Reliability

I bet there is no known trivial case which would lead Boost.Any to failure. But what about cdiggins::any? Unfortunately I’ve found one major issue.

Say we have two separate compiled libraries (Foo and Bar, yeah) which interchange data in the form of Any. Will policies of some type T from Foo and T from Bar match? The answer is No. These policies are different objects – this makes it totally impossible to safely cast the Any instance back to T.

Possible solution for this problem can be rather straightforward – combination of both approaches. The ‘equality miss’ case should not happen often in real apps so we can simply add a fallback procedure, which checks if types are really different in the way Boost does it:

I am pretty sure some things are disputable or unclear so I’d welcome any comments!

Posted in c++