0

There are several parts to my question. I have been researching on how/when to use boost::any. I was wondering if it is possible to assign a struct to a boost::any variable.

Example:

struct S {
   int x;
};

S s;
s.x = 5;

boost::any var = s;

It would seem to me that this is possible but it leads me to my next question. If this is a valid assignment, then how would I access the data member x? var is not a struct type since it is boost::any.

My next question is not dependent on whether the data member can be accessed or not. The question then is, what if variable a is of type uint8_t.

Example: Edit: as pointed out in the comments, the code below does support uint8_t but it is not printed. See uint8_t can't be printed with cout.

uint8_t a = 10;

boost::any b = a;

std::cout << boost::any_cast<uint8_t>(b);

I found that it is possible to use boost::any_cast but have not found that it supports unsigned types. When I tried using boost::any_cast<uint8_t>() it did not print, but did not throw an error. Is it possible to get the value of a type like uint8_t using boost? If so how?

I will continue reading more documentation of boost::any but if anyone has insight, details, or notes on these questions or the subject, please post as I would love to learn more about how this works. Thanks!

demogorgon
  • 474
  • 4
  • 20
  • Best way to be sure is to try it. I don't see any issue with uint8_t except pre-c++11 where it could be aliased to char. The principle of any is type erasure. google it – Michael Doubez Jun 27 '18 at 22:13
  • As described in the documentation you linked to, the conversion is via `any_cast`, not `cast_any`. If that is a typo in your question, please fix it. If that's a typo in your code, then maybe that's what one of your problems is. – JaMiT Jun 27 '18 at 22:34
  • @JaMiT yes that is a typo, thank you. – demogorgon Jun 27 '18 at 22:35
  • 1
    casting to `uint8_t` with `any_cast` should work. You may be having [this](https://stackoverflow.com/questions/19562103/uint8-t-cant-be-printed-with-cout) problem – kmdreko Jun 27 '18 at 22:42
  • @vu1p3n0x I think you are correct. Since I couldn't see the value, I used `sizeof` on `any_cast` and saw that it was 1. So I think the value is there.. thanks for input – demogorgon Jun 27 '18 at 22:53

1 Answers1

2

I was wondering if it is possible to assign a struct to a boost::any variable

It is.

How would I access the data member x?

You would access with any_cast<S>(var).x. Continuing your example:

int& the_x_member = any_cast<S>(var).x;
std::cout << "s.x is " << the_x_member << "\n";

What if variable a is of type uint8_t?

It's perfectly possible to assign an unsigned integer type to a boost::any (or std::any, which does the same thing but with somewhat different syntax).

When I tried using boost::any_cast<uint8_t>() it did not print, but did not throw an error.

Wouldn't that "print" a \0 character? So it would look like nothing was printed.

Is it possible to get the value of a type like uint8_t using Boost? If so how?

Just like you'd expect:

uint8_t u = 234;
boost::any ba = u;
std::cout << "u is " << (int) boost::any_cast<uint8_t>(ba) << '\n';

This does indeed work.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Thanks so much for the insight, extremely helpful. I realize my second example is misleading using `x` again and it makes it seem that I did in fact think it was a data member still. I will edit that so it is more clear for future readers. Much appreciated! – demogorgon Jun 27 '18 at 23:32