Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #13774 > unrolled thread

returning a type

Started by"U.Mutlu" <for-usenet@mutluit.com>
First post2012-02-05 21:52 +0100
Last post2012-02-08 17:40 -0800
Articles 5 — 5 participants

Back to article view | Back to comp.lang.c++


Contents

  returning a type "U.Mutlu" <for-usenet@mutluit.com> - 2012-02-05 21:52 +0100
    Re: returning a type LR <lruss@superlink.net> - 2012-02-05 22:23 -0500
    Re: returning a type Juha Nieminen <nospam@thanks.invalid> - 2012-02-06 06:25 +0000
    Re: returning a type Paavo Helde <myfirstname@osa.pri.ee> - 2012-02-06 01:24 -0600
      Re: returning a type 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-08 17:40 -0800

#13774 — returning a type

From"U.Mutlu" <for-usenet@mutluit.com>
Date2012-02-05 21:52 +0100
Subjectreturning a type
Message-ID<jgmq6i$bmk$1@news.albasani.net>
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.

[toc] | [next] | [standalone]


#13776

FromLR <lruss@superlink.net>
Date2012-02-05 22:23 -0500
Message-ID<4f2f47e6$0$10054$cc2e38e6@news.uslec.net>
In reply to#13774
U.Mutlu wrote:
> 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/

[toc] | [prev] | [next] | [standalone]


#13777

FromJuha Nieminen <nospam@thanks.invalid>
Date2012-02-06 06:25 +0000
Message-ID<4f2f7270$0$3062$7b1e8fa0@news.nbl.fi>
In reply to#13774
U.Mutlu <for-usenet@mutluit.com> wrote:
> 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.)

[toc] | [prev] | [next] | [standalone]


#13778

FromPaavo Helde <myfirstname@osa.pri.ee>
Date2012-02-06 01:24 -0600
Message-ID<Xns9FF15FC826D0Emyfirstnameosapriee@216.196.109.131>
In reply to#13774
"U.Mutlu" <for-usenet@mutluit.com> wrote in
news:jgmq6i$bmk$1@news.albasani.net: 

> 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.

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.

> 
> 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.

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

> 
> 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.

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

[toc] | [prev] | [next] | [standalone]


#13861

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-02-08 17:40 -0800
Message-ID<1827753.40.1328751638662.JavaMail.geo-discussion-forums@pbbok8>
In reply to#13778
在 2012年2月6日星期一UTC+8下午3时24分55秒,Paavo Helde写道:
> "U.Mutlu" <for-usenet@mutluit.com> wrote in
> news:jgmq6i$bmk$1@news.albasani.net: 
> 
> > 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.
> 
> 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. 

 
> > 
> > 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.
> 
> Not directly. However, again you can do this with the same kind of 
> template dispatch as above, using string comparison instead of enum 
> comparisons.
> 
> > 
> > 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.
> 
> 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web