I put positive 'int' to my any variable. Then I am trying to cast it to 'unsigned long'. And I got an exception about bad any cast. How I can make the use of 'boost::any_cast' more flexible conserning integer data.
Asked
Active
Viewed 726 times
0
-
2You can't. `boost::any` is not very flexible in this regard. The types have to match exactly. If you have a known list of types, I'd suggest looking into `boost::variant` instead. – Chad Dec 12 '12 at 00:08
-
1By the way, boost::any is horribly inefficient for holding integers. Each any entry uses a pointer in addition to the value itself. You'd be better off just making an array of the largest integer type. – Zan Lynx Dec 12 '12 at 00:16
1 Answers
4
Extract it into a temporary variable and then cast that to the integer type that you want.

Zan Lynx
- 53,022
- 10
- 79
- 131
-
Problem that where I apply boost::any_cast, I don't know what type it was the former type. – user14416 Dec 12 '12 at 15:16
-
@user14416: You can't use boost::any that way. You must know the type, or at least work your way through a list of the possible types. – Zan Lynx Dec 12 '12 at 16:35