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


Groups > comp.lang.c++ > #13861

Re: returning a type

From 88888 Dihedral <dihedral88888@googlemail.com>
Newsgroups comp.lang.c++
Subject Re: returning a type
Date 2012-02-08 17:40 -0800
Organization http://groups.google.com
Message-ID <1827753.40.1328751638662.JavaMail.geo-discussion-forums@pbbok8> (permalink)
References <jgmq6i$bmk$1@news.albasani.net> <Xns9FF15FC826D0Emyfirstnameosapriee@216.196.109.131>

Show all headers | View raw


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

Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

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

csiph-web