Pulling the type declaration out of a variable?

S

Steven T. Hatton

Somewhere I seem to recall an example where the author was able to access
some kind of representation of a variable's type and use it to create
another variable of the same type. It may have been in the context of
templates.

Here's why I ask; I have a member field declared like this:

protected:
vector<vector<SbVec3f*> >* strips_ptr;

Within a function, I came across a reason to want a local variable of the
same type. The alternatives are to either use the same long (painful to
type) declaration, or use a typedef. I personally dislike typedefs in most
situations. They seem like just another level of indirection with the
potential to obfuscate and confuse.

OTOH, if I can create a variable in such a way that I can see it's another
instance of one I'm already using, that seems more informative and direct.
 
G

Gianni Mariani

Steven said:
Somewhere I seem to recall an example where the author was able to access
some kind of representation of a variable's type and use it to create
another variable of the same type. It may have been in the context of
templates.

Here's why I ask; I have a member field declared like this:

protected:
vector<vector<SbVec3f*> >* strips_ptr;

Within a function, I came across a reason to want a local variable of the
same type. The alternatives are to either use the same long (painful to
type) declaration, or use a typedef. I personally dislike typedefs in most
situations. They seem like just another level of indirection with the
potential to obfuscate and confuse.

OTOH, if I can create a variable in such a way that I can see it's another
instance of one I'm already using, that seems more informative and direct.


The way to extract a type using standard C++ is by using template
functions. However, gcc does have an extension called "typeof" which
allows you to do type extraction without resorting to complex templates.
typeof is not supported universally.

If you can place your algorithm that needs a local variable of your
particular type in a template function then this is pretty easy.

template <typename T> void func( T input )
{
T local_variable;
}

calling "func( strips_ptr )" gives you that local variable.
 
C

Chris \( Val \)

| Somewhere I seem to recall an example where the author was able to access
| some kind of representation of a variable's type and use it to create
| another variable of the same type. It may have been in the context of
| templates.
|
| Here's why I ask; I have a member field declared like this:
|
| protected:
| vector<vector<SbVec3f*> >* strips_ptr;
|
| Within a function, I came across a reason to want a local variable of the
| same type. The alternatives are to either use the same long (painful to
| type) declaration, or use a typedef. I personally dislike typedefs in most
| situations. They seem like just another level of indirection with the
| potential to obfuscate and confuse.
|
| OTOH, if I can create a variable in such a way that I can see it's another
| instance of one I'm already using, that seems more informative and direct.

I'm not sure that I understand exactly what it is
your asking, however, I have to disagree with you,
that typedef' can obfuscate and confuse - I think
that they are a good thing, and do exactly what they
were intended to do quite nicely.

Cheers.
Chris Val
 
J

JKop

Steven T. Hatton posted:
Within a function, I came across a reason to want a local variable of
the same type. The alternatives are to either use the same long
(painful to type) declaration, or use a typedef. I personally dislike
typedefs in most situations. They seem like just another level of
indirection with the potential to obfuscate and confuse.


typedef
MammalWithFourLegsThatBarksAndWaggsItsTailWhenItsHappyAndLowersItsEarsWhenIt
sSadOrAfraid Dog;


I don't see the problem.


-JKop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top