Sign in to follow this  
3dmodelerguy

Finding a variable type?

Recommended Posts

Well I have created an any type object from help on another thread on mine. It works fine but i would like to enhance it. Right now i can set it to any type and it work fine(tested int, and string quickly). Now am not sure it this is even possible since i have been use PHP solely for the past 2 years so let me know if this is even possible. is there a way the this any type object can know what type it is and pass back the correct type of variable automatically. here is an example of what i have to do right now and want i would like to be able to do:
//what i have to do
variable* var = new variable(new int(4));
int var_value - var->get<int>();

variable* var2 = new variable(new std::string("test"));
int var2_value - var2->get<std::string>();

delete var;
delete var2;

//what i would like to do
variable* var = new variable(new int(4));
int var_value - var->get();

variable* var2 = new variable(new std::string("test"));
int var2_value - var2->get();

delete var;
delete var2;



so i basic want to be able to automate the processing of get the value stored to where i don't have to know what type it is currently storing.

Share this post


Link to post
Share on other sites
Quote:
Original post by 3dmodelerguy
so i basic want to be able to automate the processing of get the value stored to where i don't have to know what type it is currently storing.


Use PHP?

In C++, this type of behaviour generally isn't viable. If you must use it from C++, integrate Python or Lua scripting.

The answer (proper one) to designing such a system (in C++) is simply too extensive to fit into a single reply. There are so many problems with everything related, that the existing (correct) solution would need to cover for either thousands (or infinite) number of combinations (const correctness, reference/pointer semantics). Even boost::function chooses to omit certain conversions for this very reason (it would require hundreds of function signatures).

Share this post


Link to post
Share on other sites
int var2_value - var2->get<std::string>();
Why does this look wrong to me? Anyway -- you need to specify an appropriate type when declaring the variable where you want to store the result -- what would omitting the type in the template-parameter-list give you? Except for less typing, which I understand. But then, you could do something like
template < typename T >
void assign( variable< T > const& var, T& t ) {
t = var.get< T >();
}

...

variable var1( new int( 4 ) );
int var1_value;
assign( var1, va1r_value );

variable var2( new std::string( "..." ) );
std::string var2_value;
assign( var2, var2_value );

Share this post


Link to post
Share on other sites
I guess another question would be is there a way to use a variable to declare the template type like:

std::string template_type = "int";
variable* var = new variable(new int(4));
int var_value = var->get<template_type>();

delete var;


and that would know to return an int. I know that code does not work but that is basically want i want. again this might be a something hard to describe in one reply. Any links to help me with what i am trying to do would be great.

Share this post


Link to post
Share on other sites
Quote:
Original post by 3dmodelerguy
I guess another question would be is there a way to use a variable to declare the template type like:
*** Source Snippet Removed ***
and that would know to return an int. I know that code does not work but that is basically want i want. again this might be a something hard to describe in one reply. Any links to help me with what i am trying to do would be great.


I will, again, point out the excellent and almost 100% optimal implementation of what you want via cdiggins::any.

That implementation allows you to do:

try {
any x = 17;

int i = x.cast<int>();
} catch (bad_any_cast &e) {
std::cout << e.what();
}


What you're asking for ("would know to return an int") is something that is currently not possible in C++. Main reason for this is ambiguity it would create.

The next C++ standard will provide auto type, which will be resolved by compiler, allowing you to write something like this:

auto x = foo();
auto y = baz();
std::cout << (x + y);

(disclaimer: I have not looked into how exactly it will work, so the above might be wrong)

But right now, you need to know return type in advance, and you need to specify it.

It is possible to provide operations on any type, but that implementation requires n^2 function signatures, where n is number of distinct type signatures (There's dozens of them just for basic types). For example, to support int, you'd need to provide them for: int, const int, int &, const int &, int *, const int *.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this