returning a type

U

U.Mutlu

Three questions regarding type information:

1) Is it possible that a userfunction returns the type of an object
(instead of the object itself), so that it can be used in a declaration or definition?
Something like what typename(obj) does, but the factory should be a userfunc.
This would open up much advanced possibilities in the language.

2) Is it possible to declare/define a type from a string literal?
Ie. you have a const char* psz = "int" and want declare/define a variable
of that type that's in the string, ie. in this case int.

3) Is it possible to test a type that is in a string literal?
Ie. you have a const char* psz = "int" and want test if it is
a certain type, using typeid() etc.
 
S

Stefan Ram

U.Mutlu said:
2) Is it possible to declare/define a type from a string literal?
Ie. you have a const char* psz = "int" and want declare/define a variable
of that type that's in the string, ie. in this case int.

When a C++ compiler is at hand at runtime, code can be
generated and compiled to a run-time library (when support
for such libraries is available) and then loaded and called.

However, the whole idea of types is some /static/ safety,
which means that types should be fixed at write time.
Otherwise, one can just pass around void pointers and cast
them to whatever needed, when needed. (Such casts might not
be portable, but often will work.) Or one can use B or BCPL.

To implement run-time typing in OOP, the usual solution is
polymorphism (»tell, dont't ask«), which also can be used in C++.

If you would describe the problem that is to be solved,
preferably with a small SSCCE, then often someone will be
able to finde a more C++-like solution to the actual problem.
 
L

LR

U.Mutlu said:
Three questions regarding type information:

1) Is it possible that a userfunction returns the type of an object
(instead of the object itself), so that it can be used in a
declaration or definition? Something like what typename(obj) does,
but the factory should be a userfunc. This would open up much
advanced possibilities in the language.

2) Is it possible to declare/define a type from a string literal? Ie.
you have a const char* psz = "int" and want declare/define a
variable of that type that's in the string, ie. in this case int.

3) Is it possible to test a type that is in a string literal? Ie. you
have a const char* psz = "int" and want test if it is a certain type,
using typeid() etc.

It's not clear to me what you want to accomplish, but I think you may
find the Boost Variant and Python libraries useful, if not directly,
then possibly for the techniques they use. http://www.boost.org/
 
J

Juha Nieminen

U.Mutlu said:
Three questions regarding type information:

1) Is it possible that a userfunction returns the type of an object
(instead of the object itself), so that it can be used in a declaration or definition?
Something like what typename(obj) does, but the factory should be a userfunc.
This would open up much advanced possibilities in the language.

2) Is it possible to declare/define a type from a string literal?
Ie. you have a const char* psz = "int" and want declare/define a variable
of that type that's in the string, ie. in this case int.

3) Is it possible to test a type that is in a string literal?
Ie. you have a const char* psz = "int" and want test if it is
a certain type, using typeid() etc.

The answer to all three questions is no.

The closest language that has full support for all that is Objective-C.
(OTOH that language lacks many of the strengths of C++, such as RAII,
templates, constructors, inner types/classes, etc.)
 
8

88888 Dihedral

在 2012å¹´2月6日星期一UTC+8下åˆ3æ—¶24分55秒,Paavo Helde写é“:
Not directly. A type in a declaration or a definition is always fixed at
compile-time in C++ and cannot be changed by a run-time decision.
However, with templates you can emulate this to some extent, as with a
template you can generate code for multiple different types easily. At
some point one needs some "template dispatch" code which effectively
translates the run-time choice into what compile-type is used:

template<typename T> void f() {
T x; // here is your definition
}

// dispatch according to the type selected ad run-time
// this can be an enum value, for example:

switch(SelecType()) {
case k_int_type:
f<int>();
break;
case k_double_type:
f<double>();
break;
case k_A_type:
f<A>();
break;
}

If one has many such dispatch points with the same set of supported
types, one can define some nifty preprocessor macros to avoid repeating
the tedious dispatch code all over.
Well, I knew this trick long time ago when I had to
decode image file formats of different kinds.

Not directly. However, again you can do this with the same kind of
template dispatch as above, using string comparison instead of enum
comparisons.


type_info::name() gives you a string representation of the type, but the
actual string content is very implementation specific and can change in
the next version of the compiler, which severely limits its utility.

hth
Paavo

There was something different to allow written tested factored functions
to be able to be applied in a new format added lator.
 

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,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top