0

The any_cast() function - say, for a non-const reference operand - has the following signature:

template<class T>
T any_cast(any& operand);

Why does this function not return a T& - where the documentation says the returned value is merely

static_cast<T>(*std::any_cast<U>(&operand))

?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • If you want a ref, cant you just do `T& val = any_cast(any);`? Seems to me if you ask for a T, you should get a T (and not a T&). – Mike Vine Jul 09 '20 at 15:36
  • There are things in C++ which you _cant_ create a reference to, and returning a reference to T would forbid those types in an any. Although they may be forbidden already. – Mike Vine Jul 09 '20 at 15:40
  • For instance http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2015/p0172r0.html – Mike Vine Jul 09 '20 at 15:40
  • @MikeVine: Would `any_cast` even work? I know `any_cast` is very very sensitive. Also, if I can get an address - I can get a reference. – einpoklum Jul 09 '20 at 15:43

0 Answers0