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 7086643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:34:30+00:00 2026-05-28T07:34:30+00:00

Before i start i will divide the problem into two parties: PART 1 :

  • 0

Before i start i will divide the problem into two parties:

PART 1 :



In c++ to get type of data we can use typeid but it’s give you the data as const char* ,and i want it to return the type of the data.

Example:

int data = 20 ; 
float data2 = 3.14 ; 
char *data3 = "hello world" ;

std::cout<< typeid(data).nam() << endl << endl ;
std::cout<< typeid(data2).nam() << endl << endl ;
std::cout<< typeid(data3).nam() << endl << endl ;

Now i have a function that get data from void* , and convert it to another type :

template <typename t >
void print (void *data )
{   

    boost::any _t = static_cast<t> (data);
    cout << boost::any_cast<t> (_t) << endl << endl;
}

Now this works fine if you know your data type:
Example:

void *mydata = alloca(size_object) ;
void some_function_store_int_data_in_voidpointer( &mydata)
print <int> (mydata); // it's ok .

But this is impractical when you have lots of different datatypes, like this:

void somefunction(args &a , void *dest  )
{
 /*code returnd data */
}
enum args
{
_INT_ ,
_FLOAT_ , 
_CHARPOINTER_ ,
};

vector <void *test> myvector ;
myvector.resize (3) ;


void somefunction(_INT_ , myvector.at(0)  ) ;  // store int in void*
void somefunction(CHARPOINTER , myvector.at(0)  ) ;// store char* in void*
void somefunction(_FLOAT_ , myvector.at(0)  ) ;// store float in void*

print <int> (myvector.at(0));
print <char*> (myvector.at(1));
print <float> (myvector.at(2));

1 – If i use something like this

print <typeid(myvector.at(2))> (myvector.at(2));

i get an error because my data is float and I make it const char*

2 – Perhaps I can pass the type of every value if I have few data. This is OK. But what if I have 100 values from different types!

I am looking for something like: typeid but it’ return the type not `const char*.

PART 2



because I have avector I will use a for_each algorithm like this:

for_each ( myvector.begin() , myvector.end() , print</*what i should pass her int , float ,char* ...or what , */>);

In the previous code I can pass only one type to the function so the data from the same type will print. Else the data that are not the same type will print, but completely wrong (Strange format).

So if I pass char* the int data will print completely wrong.

How can I do this differently?

  • 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-28T07:34:31+00:00Added an answer on May 28, 2026 at 7:34 am

    How can I do this differently?

    If your intention is to use same function for printing different data formats, then you can do it like this:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    template <typename T> class Callback{
    public:
        void operator()(const T& value) const{
            std::cout << value << std::endl;
        }
    };
    
    template <typename T> Callback<typename T::value_type> makeCallback(const T&){
        return Callback<T::value_type>();
    }
    
    int main(int argc, char** argv){
        std::vector<int> ints(20);
        std::vector<float> floats(20);
    
        std::fill(ints.begin(), ints.end(), 0);
        std::fill(floats.begin(), floats.end(), 0.0f);
    
        std::for_each(ints.begin(), ints.end(), makeCallback(ints));
        std::for_each(ints.begin(), ints.end(), makeCallback(floats));
    
        return 0;
    }
    

    However, if you want to store several different data types in same std::vector, then you need “variant” types (like boost::variant, QVariant or similar), and there’s no way around it.


    I am looking for something like: typeid but it’ return the type not `const char*.

    In C++ “type” exists only at compilation stage, so you cannot return it, because it no longer exists once program has been compiled. There’s no “type”, so you can’t return it.

    So to get a “type” from object you need to implement some kind of “variant” type that can hold any object along with its type information, and pass that “variant” type around. One example of such system is QVariant in Qt 4.

    AFAIK implementation of variant type goes like this: there is some kind of table for every type variant supports, you register all types variant class must support in that table. Table provides functions for creating type, destroying type, (de)serializing type, and possibly information about amount of memory required by one object of the type. The table can contain optional information you want, and you can convert entire registration procedure into macros+template combo. As you can see, this is not something that is done automatically by compiler, but something that involves plenty of hassle and must be taken care of by programmer. Also, things get much more fun if program must be able to take types developed externally (in plugins, etc).

    As a result of language restrictions, the better idea would be to avoid situations when you need to “return type” when possible – variant systems aren’t exactly difficult, but they aren’t much fun either, due to all necessary sanity checks and conversions. Example problem: if you pass a string in variant type to a function that is supposed to take a float, should this function attempt to convert string to float? If conversion fails, should it crash/throw exception, or assume that variable has default value? If there’s default value for failed conversions, what should it be and how should it be passed? And so on. This isn’t a rocket science, but it is quite annoying to deal with.

    For example, you could get rid of “void*” (if function takes pointer as an argument, then I would assume that poitner can be NULL/0. So “void*” arguments aren’t exactly a good idea). arguments in your functions and use templates to make compiler generate code your want for types you actually use in your program. If templates are not an option, then you need some kind of “variant” type (preferably developed by somebody else), … or you could switch to another language that provides type information you need. You don’t have to use C++, any tool that does the job will do. Relying on RTTI also isn’t a perfect solution, becuase if you manage to pass a pointer to something that does NOT contain type information, you’ll get a non-standard exception (__non_rtti_object).

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

Sidebar

Related Questions

Hey, Before I start to write my problem, I will excuse for my bad
Before I start explaining the code I will first give my use case so
I want to get one record before start date and end date DtpFrom means
Before I start let me ask that in objective-c 2.0 you can have a
Before start let me tell my experience: I am experienced with C#.NET, web services,
I usually try to do TDD with not much analysis (no diagrams) before start
Before I start, I know there is this post and it doesn't answer my
Before you start firing at me, I'm NOT looking to do this, but someone
Before I start developing a desktop application, I was wondering how large the installer
Before I start with the real question, let me just say that I might

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.