Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7191293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:41:56+00:00 2026-05-28T19:41:56+00:00

I need to cast an any variable to an original type. I need to

  • 0

I need to cast an any variable to an original type.
I need to do this:

int i = 10;
any a(i);
int& i2 = any_cast<int &>(a);

But I want that the type stores in any variable. And I write this:

int i = 10;
any a(i);
a::type_value& i2 = any_cast<a::type_value &>(a); // there is no actually type_value

How can I do something like that? Or how can I extract the original type from the any variable? Boost.variant is convenient either.

If I cannot do that, then I have another question what C++ techniques and libraries can store and get the type through a function to solve this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:41:57+00:00Added an answer on May 28, 2026 at 7:41 pm

    C++ is a statically typed language. The type of a boost::any is a runtime value; any particular any could have any type. That’s kinda the point.

    There is no any::type_value, because that would have to be a compile time value. And any is a runtime construct.

    Consider this:

    void TakeAnAny(boost::any a)
    {
      a::type_value& i2 = any_cast<a::type_value &>(a);
    }
    

    What type is any::type_value? It is legal to call TakeAnAny with virtually any type. There is no single compile-time type that any::type_value could reduce to. And therefore, there is no way for the compiler to determine a type. Since C++ is statically typed, you’re hosed.

    The ultimate purpose of any is type-erasure. I have some value. And I want to pass that to some other function. This process will go through several different communication layers. But I don’t necessarily want all of those different layers to know exactly what type I’m using. I only need myself and my intended destination to know the type. So you stick it in an any and you’re fine. Everyone else just sees the any, and both of you know what it wraps.

    This process only works because both the source and the destination know the real type of the value. If you don’t know the type, then you shouldn’t be using any. The purpose of any is not have a function sit there and cast it to a bunch of possible types (that’s what boost::variant is for). The purpose is to erase the type from a function’s signature.

    This allows for things like generic messages and signals. You register some event handler with a system. You fire an event that takes an any as a parameter. The person firing the event knows that the “MouseClick” event always takes a vec2 as its parameter. So every “MouseClick” handler casts it to a vec2. The “KeyPress” event would maybe pass a int32_t. So those handlers cast it to that type. And so forth. Everyone knows what type it actually takes.

    This used to be done with void*. The problem there is that you have ownership issues (any is a value, while void* is a pointer). Also, a void* is so type-erased that there’s no way to check to see if your cast is correct. any is really just a type&value-safe void*; it prevents you from casting to the wrong type.

    You don’t really want any. Your use case doesn’t seem to want variant either. What you seem to want is a template. That’s a different kind of thing, and it would let you do what you really want: have a function that can use any particular type, while still being able to know exactly what that type is.

    Of course, templates have their own limitations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a decimal number like this: 62.000,0000000 I need to cast that decimal
String[] a = c.toArray(new String[0]); First: Do I need type cast here? (I think
How to cast an object (of type Object) into its real type? I need
In my Java program, I need to cast an object of type List<? extends
I need to cast a generic VBA form control object into a ComboBox object
I need to cast: IQueryable<PARENT> to IQueryable<Child> . IQueryable<PARENT> query = GetParents(); IQueryable<CHILD> result
For some reason I need to cast/convert a DateTime into one of many custom
Well, I have a class Customer (no base class). I need to cast from
Need a function that takes a character as a parameter and returns true if
Need to an expression that returns only things with an I followed by either

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.