The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

C++ question!

C++ question!

Suppose I have an object obj. I want to get its type and instantiate another
object of the same type during compile time. I.e., something like this,

    TYPE(obj) another_obj;

Is there any meta-programming wizadry that can do this?
Leon
Sunday, February 05, 2006
 
 
As far as I know: in standard c++ no.
foldip
Sunday, February 05, 2006
 
 
If you give all your classes a factory() method that looks something like:

class CObj()
{
public CObj *factory()
{
return new CObj();
}

};

Then your problem is pretty much solved.

void *newobjectoftype = objectoftype.factory();
A.A.
Sunday, February 05, 2006
 
 
You want to instantiate at compile time?

I think you are looking for a runtime method, line clone or factory.create
B Send private email
Sunday, February 05, 2006
 
 
If the obj class has a suitable copy constructor you could copy it (at times this is called cloning).


/Allan
Allan Wind Send private email
Sunday, February 05, 2006
 
 
Thanks for the replies. The requirement actually has nothing to do with cloning or factory methods. I need the type of a given object at compile time, that's all!
Leon
Sunday, February 05, 2006
 
 
RTTI gives you this. You may have to set a flag in your compiler.
Art Wilkins
Sunday, February 05, 2006
 
 
" I need the type of a given object at compile time, that's all!"

That's a very strange request. Why don't you know it already from looking at the type of the variable? What are you trying to accomplish?

More information would be useful.
clcr
Sunday, February 05, 2006
 
 
For runtime relection, this might help.
http://seal-reflex.web.cern.ch/seal-reflex/index.html

For compile time, I don't think C++ has what you need.

I would be great if you could say:

Foo x;
Object<typeid(x)> obj;

And then obj and x would be of the same type. But you can't.
MBJ Send private email
Sunday, February 05, 2006
 
 
Yeah, C++ doesn't provide any such operator, but I was hoping some meta-programming trick might be able to do it. Anyway, the requirement is not severe enough to waste any more time on it either!

Thanks again.
Leon
Sunday, February 05, 2006
 
 
Wow, why do I even bother to answer.
Art Wilkins
Sunday, February 05, 2006
 
 
AW, RTTI as the name says gives you the Rumtime Type Information. I needed the type at compile time from the object.

Not an extremely critical thing - just that my code is getting cluttered immensely with things like map< pair<int, AnotherClass<string> >, ..etc. >::iterator itr, when all I want to say is, "itr is an iterator of theMap we're currently dealing with". I.e., Something like

TYPE<theMap>::iterator itr.

I *could* use typedefs, but one gets tired of either resolving the meaning of typedefs/ or searching for meaningful aliases.

I do appreciate your taking time to consider my question. Consider how many man-years we the coders of the world collectively save thus... :-)
Leon
Monday, February 06, 2006
 
 
> Rumtime

LOL!
Leon
Monday, February 06, 2006
 
 
Annoyingly, the ability to ask an object for it's static type is missing from C++, I suspect because it would be hard to define clearly.

I'm guessing the point of this is so that if your original object changes type, you don't have to go changing all the mentions of objects which have the same type.

You can do that fairly easily using a quick typedef though... Instead of declaring;

Class Blah { .. };

Blah originalVariable;

<lots of code>

Blah anotherVariable;

And then if you change the type of originalVariable having to find all the mentions, you say

typedef Blah ORIG_VAR_TYPE;
ORIG_VAR_TYPE originalVariable;

<lots of code>

ORIG_VAR_TYPE anotherVariable;
Katie Lucas
Monday, February 06, 2006
 
 
"TYPE<theMap>::iterator itr."

Yeah, OK, you're after those type entries;

I just use macros to generate the typedefs, and the typedefs live inside the classes which contain the maps (so they don't clash).

You just want something which says

#define CONTAINER(NAME,ACTUALTYPE) typedef ACTUALTYPE NAME; typedef ACTUALTYPE::iterator NAME##_ITER; typedef ACTUALTYPE::const_iterator NAME_CONST_ITER;

And then drop one of those in where you want the container defining.

CONTAINER(MAP_STRS,map<string>);
MAP_STRS myMap;
Katie Lucas
Monday, February 06, 2006
 
 
Macros are yucky and have this sort of sniffyness surrounding them ever since various languages (Java, I'm looking at you) made these sweeping definitions about them being Not Needed.

They're absolutely right, you don't need them. You just have to enjoy your extra typing instead...
Katie Lucas
Monday, February 06, 2006
 
 
> Rumtime

Captain Morgan?
MBJ Send private email
Monday, February 06, 2006
 
 
Some C++ compilers support a typeof operator that can be used at compile time e.g.

int* i;
typeof (i) j; // j has the same type as i

In C++ an expression has an unambiguous type so typeof has a definite result.

In the absence of typeof support in your compiler, there are hacks involving SFINAE. For full generality, it involves ickiness like assigning a Godel number to each and every possible type in C++, then doing some overloads based on the standard C++ operator sizeof.

http://www.semantics.org/code.html#typeof
Glen Low
Tuesday, February 07, 2006
 
 
A typedef is the generally accepted solution to this problem. One other approach is to use some sort of traits template:

template< class T > struct ContainerTraits {
  typedef T::iterator iterator;
};

Then you could do something like:

ContainerTraits< map< ... > >::iterator i = ...;

but that doesn't really save any typing, does it?
Chris Tavares Send private email
Tuesday, February 07, 2006
 
 
"Wow, why do I even bother to answer."

Wow, why do you follow up with a sarky note after giving a wrong answer.
Kasey
Saturday, February 11, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz