Search code examples
c++stdstdany

std::any - why does it lack so many operators?


I am looking at using std::any for type erasure as part of an interface I am building, and I am left wondering why it is lacking so many operators, like "+", "==". I tinkered on Coliru here, where I extended std::any to be summable and printable into an ostream. This is only for anys holding double right now, but the concept works, it is just implementation that would need to be done to support all basic numeric data types (as long as lhs and rhs are the same type for operands).

Considering I was able to extend any like this in this short time, why is the stl implementation so limited when it comes to operands?

Any thoughts are appreciated.


Solution

  • Because in full generality (which is what std::any aspires to) these operators don't make sense. With regards to your specific examples;

    +: Meaningless for date-like objects.

    ==: Meaningless for write-only objects.