Storing a type in C++

Is it possible to store a type name as a C++ variable? For example, like this:

type my_type = int; // or string, or Foo, or any other type
void* data = ...;
my_type* a = (my_type*) data;

I know that 99.9% of the time there’s a better way to do what you want without resorting to casting void pointers, but I’m curious if C++ allows this sort of thing.

storing data type in c

I can’t find anything on the net that tells how to put a data type into a pre-allocated block of memory. I hate doing memory management myself, but in this case I must. I have a block of memory that i

Storing “binary” data type in C program

I need to create a program that converts one number system to other number systems. I used itoa in Windows (Dev C++) and my only problem is that I do not know how to convert binary numbers to other nu

C++ data type for storing large number

I have a program whihch needs to store several numbers. The largest one can be of the order 10^15. How should I go about storing the number. I’m using Gcc 4.3.2 compiler.

storing characters in char data type C language

I can store string of few length in char data type. But when it exceeds its capacity what can be the alternative way to store string. I am using char data type. void setString(char* inPoints) { if (s

proper/best type for storing latitude and longitude

In a system level programming language like C, C++ or D, what is the best type/encoding for storing latitude and longitude? The options I see are: IEEE-754 FP as degrees or radians degrees or radians

Data type for storing object pair other than Dictionary in C#

What would be the best data type for storing non-unique pairs of objects? I cannot use Dictionary because the key has to be unique. I could create an object with the two data types as properties and s

Julia, type storing itself

So here is the code I have abstract AbstractNode type Edge source::AbstractNode target::AbstractNode Edge(source::AbstractNode, target::AbstractNode) = new(source, target) end type Node <: Abstract

VB.NET best data type for storing currency values

What is the most appropriate data type for storing currency values in VB.NET?

C++0x : Storing any type of std::function in a std::map

I’m trying to store a set of std::function in a map (under GCC 4.5) I’d like to get 2 kind of things : storing functions with arguments already passed; then you just have to call f() storing function

storing a double type value from html textbox into a c# variable

Does anyone know that how to store a double type value from html textbox into a c# variable? I have a html textbox with Id txtCost and that will hold a double value. Thanks in advance.


No, this is not possible in C++.

The RTTI typeinfo operator allows you to get some information about types at runtime: you can get the type’s name and check whether it is equal to another type, but that’s about it.

Types are not objects in C++ (where they are in Ruby, for instance), so you cannot store instances of a type. Actually, types never appear in the executing code (RTTI is just extra storage).

Based on your example, it looks like you’re looking for typedefs.

typedef int Number;
Number one = 1;
Number* best = (Number*) one;

Note that a typedef isn’t storing the type; it is aliasing the type.

No you can’t store the type directly as you want, but you can instead store the name of the type.

const char* str = typeid(int).name();

I guess whenever you planned to use that variable for comparison, you could instead at that time compare the str variable against the name() of the types.

const char* myType = typeid(int).name();


//Some time later:
if(!strcmp(myType, typeid(int).name()))
  //Do something

More info available here

Yes, if you code it yourself.

enum Foo_Type{

struct MyFoo{
    Foo_Type m_type;
    Boost::shared_ptr<Foo> m_foo;

as commented below, what I left out was that all these “foo” types would have to be related to Foo. Foo would, in essence, be your interface.

You can’t do that in C++, but you can use the boost any library then test for the type it holds. Example:

bool is_int(const boost::any & operand)
  return operand.type() == typeid(int);

Not as written, but you could do something similar…

class Type
        virtual ~Type(){}
        virtual void* allocate()const=0;
        virtual void* cast(void* obj)const=0;

template<typename T> class TypeImpl : public Type
         virtual void* allocate()const{ return new T; }
         virtual void* cast(void* obj)const{ return static_cast<T*>(obj); }

// ...
Type* type = new TypeImpl<int>;
void* myint = type->allocate();
// ...

This kind of thing can be extended depending on what features you need.

A better process is to have a common base class containing a load method, and an interface for loaders. This would allow other parts of the program to load data generically without knowledge of the descendant class:

struct Load_Interface;

struct Loader
  virtual void visit(Load_Interface&) = 0;

struct Load_Interface
  virtual void accept_loader(Loader& l)

This design avoids the need to know the types of objects.

Today I had a similar problem while coding:
I had the need to store a polymoriphic data type (here named refobj) over wich call functions of the concrete classes implementing it. I need a solution that doesn’t cast the variable explicitly because I need to reduce the amount of code.

My solution (but I haven’t tested it yet) looks similar to a previous answer. Actually is quite an experimental solution. It look like this…

// interface to use in the function

class Type   
    virtual void* getObj()const=0;

// here the static_cast with the "stored" type

template<typename T> class TypeImpl : public Type
    TypeImpl(T *obj) {myobj=obj;}
    virtual void* getObj()const{ return static_cast<T*>(myobj); }

    T* myobj;

// here the type that will contain the polimorific type
// that I don't want to cast explicitly in my code
Type *refobj;

// here the "user code "
void userofTypes()
    ( refobj->getObj() ).c_str(); 
    // getObj() should return a string type over which
    // calling string concrete functions ...let's try!

void main()
    refobj=new TypeImpl < string > ( new string("hello") );
// it might seem absurd don't cast refobj explicitly, but of
// course there are situation in which it can be useful!