PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > how can i short name a compile time state ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
how can i short name a compile time state ?

Réponse
 
LinkBack Outils de la discussion
Vieux 25/02/2008, 07h36   #1
toton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut how can i short name a compile time state ?

Hi,
I want to remember a compile time variable state, just to have some
typing convenience. I am not sure if I can do it using some typedef.
To give a short example,
I have an enum as,

enum dir_type{
dir_x,dir_y,dir_xd
};

and 2 specializations as,

template<dir_type>
dir_type o_dir();

template<>
dir_type o_dir<dir_x>(){
return dir_y;
}
template<>
dir_type o_dir<dir_y>(){
return dir_x;
}

now in a function I want to do,

template<dir_type dt>
void funct(){
std::cout<<dt<<std::endl;
dir_type od = o_dir<dt>(); /// what would be compile time equiv of
this line so than i can call next line?
dir_type d = o_dir<od>();
}
Now for the commented line, I want a compile time typedef , so that od
is a name rather than a runtime state, and I can call the next line.
This is just to remove repetitive writing of o_dir<dt> , in several
places when i can simply write od.

thanks
abir
  Réponse avec citation
Vieux 25/02/2008, 08h55   #2
Michael DOUBEZ
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how can i short name a compile time state ?

toton a écrit :
> Hi,
> I want to remember a compile time variable state, just to have some
> typing convenience. I am not sure if I can do it using some typedef.
> To give a short example,
> I have an enum as,
>
> enum dir_type{
> dir_x,dir_y,dir_xd
> };
>
> and 2 specializations as,
>
> template<dir_type>
> dir_type o_dir();
>
> template<>
> dir_type o_dir<dir_x>(){
> return dir_y;
> }
> template<>
> dir_type o_dir<dir_y>(){
> return dir_x;
> }
>
> now in a function I want to do,
>
> template<dir_type dt>
> void funct(){
> std::cout<<dt<<std::endl;
> dir_type od = o_dir<dt>(); /// what would be compile time equiv of
> this line so than i can call next line?
> dir_type d = o_dir<od>();
> }
> Now for the commented line, I want a compile time typedef , so that od
> is a name rather than a runtime state, and I can call the next line.
> This is just to remove repetitive writing of o_dir<dt> , in several
> places when i can simply write od.


You cannot use a runtime variable (od) to specialize your template
(o_dir). If your program really has the form you gave (i.e. it returns
fixed values), you can transform your functions into mapping structures:

template<dir_type>
struct o_dir();

template<>
struct o_dir<dir_x>
{
static const dir_type value = dir_y;
}

template<>
struct o_dir<dir_y>
{
static const dir_type value = dir_x;
}

template<dir_type dt>
void funct()
{
std::cout<<dt<<std::endl;
static const dir_type od = o_dir<dt>::value;
/* static const */dir_type d = o_dir<od>::value;
}

Michael
  Réponse avec citation
Vieux 25/02/2008, 08h56   #3
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how can i short name a compile time state ?

On Feb 25, 8:36 am, toton <abirba...@gmail.com> wrote:

> I want to remember a compile time variable state, just to have some
> typing convenience. I am not sure if I can do it using some typedef.
> To give a short example,
> I have an enum as,


> enum dir_type{
> dir_x,dir_y,dir_xd
> };


> and 2 specializations as,


> template<dir_type>
> dir_type o_dir();


> template<>
> dir_type o_dir<dir_x>(){
> return dir_y;}


> template<>
> dir_type o_dir<dir_y>(){
> return dir_x;
> }


> now in a function I want to do,


> template<dir_type dt>
> void funct(){
> std::cout<<dt<<std::endl;
> dir_type od = o_dir<dt>(); /// what would be compile time equiv of
> this line so than i can call next line?
> dir_type d = o_dir<od>();}


> Now for the commented line, I want a compile time typedef , so
> that od is a name rather than a runtime state, and I can call
> the next line. This is just to remove repetitive writing of
> o_dir<dt> , in several places when i can simply write od.


As you've written it, you can't. od is a value, initialized by
the return value of a function, and as such, is not a compile
time constant (at least not yet---there's some discussion of
allowing such trivial functions to be used in constant
expressions).

I'm not too sure what you're trying to achieve. Perhaps
something like:

template< dir_type >
struct o_dir ;
template<>
struct o_dir< dir_x >
{
static dir_type const other = dir_y ;
} ;
// etc.

could be used, e.g.:

template< dir_type dt >
void
funct()
{
dir_type const od = o_dir:ther ;
dir_type d = o_dir< od >:ther ;
// ...
}

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Vieux 27/02/2008, 06h12   #4
toton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how can i short name a compile time state ?

On Feb 25, 1:56 pm, James Kanze <james.ka...@gmail.com> wrote:
> On Feb 25, 8:36 am, toton <abirba...@gmail.com> wrote:
>
>
>
> > I want to remember a compile time variable state, just to have some
> > typing convenience. I am not sure if I can do it using some typedef.
> > To give a short example,
> > I have an enum as,
> > enum dir_type{
> > dir_x,dir_y,dir_xd
> > };
> > and 2 specializations as,
> > template<dir_type>
> > dir_type o_dir();
> > template<>
> > dir_type o_dir<dir_x>(){
> > return dir_y;}
> > template<>
> > dir_type o_dir<dir_y>(){
> > return dir_x;
> > }
> > now in a function I want to do,
> > template<dir_type dt>
> > void funct(){
> > std::cout<<dt<<std::endl;
> > dir_type od = o_dir<dt>(); /// what would be compile time equiv of
> > this line so than i can call next line?
> > dir_type d = o_dir<od>();}
> > Now for the commented line, I want a compile time typedef , so
> > that od is a name rather than a runtime state, and I can call
> > the next line. This is just to remove repetitive writing of
> > o_dir<dt> , in several places when i can simply write od.

>
> As you've written it, you can't. od is a value, initialized by
> the return value of a function, and as such, is not a compile
> time constant (at least not yet---there's some discussion of
> allowing such trivial functions to be used in constant
> expressions).
>
> I'm not too sure what you're trying to achieve. Perhaps
> something like:
>
> template< dir_type >
> struct o_dir ;
> template<>
> struct o_dir< dir_x >
> {
> static dir_type const other = dir_y ;
> } ;
> // etc.
>
> could be used, e.g.:
>
> template< dir_type dt >
> void
> funct()
> {
> dir_type const od = o_dir:ther ;
> dir_type d = o_dir< od >:ther ;
> // ...
> }
>
> --
> James Kanze (GABI Software) email:james.ka...@gmail.com
> Conseils en informatique orientée objet/
> Beratung in objektorientierter Datenverarbeitung
> 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


Hi thanks for the solution. It is working, and this is what I wanted.
However one problem is that , its name causes problem with one of
runtime function defined as,
dir_type o_dir(dir_type dir);
Can I put same name struct and function definition in same name space,
or I need to rename 2 versions as different name.
(What i wanted to achieve with this is, one version which returns
orthogonal direction in run time using switch case statement, another
compile time version like const std::size_t dir_x =0; const
std::size_t dir_y = 1;
std::size_t o_dir[] = {dir_y,dir_x}; kind of lookup, except, I wanted
it in terms of enum, where values are not sequential (but finite) ,
and safe. In this version I cant make dir_y = 1000; If I make, 1000
size array is required.
another option is to use some kind of static (compile time ) map. But
I found the solution proposed by you a simpler one)

Thanks for responds
abir
  Réponse avec citation
Vieux 27/02/2008, 14h44   #5
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how can i short name a compile time state ?

On Feb 27, 7:12 am, toton <abirba...@gmail.com> wrote:
> On Feb 25, 1:56 pm, James Kanze <james.ka...@gmail.com> wrote:


[...]
> > I'm not too sure what you're trying to achieve. Perhaps
> > something like:


> > template< dir_type >
> > struct o_dir ;
> > template<>
> > struct o_dir< dir_x >
> > {
> > static dir_type const other = dir_y ;
> > } ;
> > // etc.


> > could be used, e.g.:


> > template< dir_type dt >
> > void
> > funct()
> > {
> > dir_type const od = o_dir:ther ;
> > dir_type d = o_dir< od >:ther ;
> > // ...
> > }

>
> Hi thanks for the solution. It is working, and this is what I wanted.
> However one problem is that , its name causes problem with one of
> runtime function defined as,
> dir_type o_dir(dir_type dir);
> Can I put same name struct and function definition in same
> name space, or I need to rename 2 versions as different name.


You can use the same name for both a class and a function in the
same scope, but I wouldn't. It's a hack, present only for
reasons of C compatibility, and will confuse the readers. I'd
come up with some different names. (Generally, the name of a
function should be a verb; the name of a class never is.)

> (What i wanted to achieve with this is, one version which returns
> orthogonal direction in run time using switch case statement, another
> compile time version like const std::size_t dir_x =0; const
> std::size_t dir_y = 1;
> std::size_t o_dir[] = {dir_y,dir_x}; kind of lookup, except, I wanted
> it in terms of enum, where values are not sequential (but finite) ,
> and safe. In this version I cant make dir_y = 1000; If I make, 1000
> size array is required.


I'm not sure I understand completely, but perhaps some sort of
metaprogramming trick could be used.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h48.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18847 seconds with 13 queries