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 > Restricting template parameters to signed or unsigned types, but notboth
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Restricting template parameters to signed or unsigned types, but notboth

Réponse
 
LinkBack Outils de la discussion
Vieux 07/04/2008, 17h13   #1
Damian
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Restricting template parameters to signed or unsigned types, but notboth

Hi there,

I would like to restrict a template parameter to a template functioned
to either a signed or unsigned type, but not both.

For example, the following templated function should ideally only be
instantiated with unsigned types.

template <typename unsigned T>
inline T id(T *i) {
return i;
}

void foo() {
(void)id<int>(k); // should return an error.
(void)id<unsigned int>(k); // should be fine.
}

Does anyone know how I can achieve this?

Thanks!

Best Regards,

Damian Eads
  Réponse avec citation
Vieux 07/04/2008, 18h10   #2
Ron Natalie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Restricting template parameters to signed or unsigned types,but not both

Damian wrote:
> Hi there,
>
> I would like to restrict a template parameter to a template functioned
> to either a signed or unsigned type, but not both.
>
> For example, the following templated function should ideally only be
> instantiated with unsigned types.
>
> template <typename unsigned T>
> inline T id(T *i) {
> return i;
> }
>
> void foo() {
> (void)id<int>(k); // should return an error.
> (void)id<unsigned int>(k); // should be fine.
> }
>

I don't know what algorithm you are indending to implement (your
example is contrived and invalid).

The general way is to just accept unqualified T but apply a
  Réponse avec citation
Vieux 07/04/2008, 20h44   #3
Damian
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Restricting template parameters to signed or unsigned types, butnot both

On Apr 7, 10:10 am, Ron Natalie <r...@spamcop.net> wrote:
> Damian wrote:
> > Hi there,

>
> > I would like to restrict a template parameter to a template functioned
> > to either a signed or unsigned type, but not both.

>
> > For example, the following templated function should ideally only be
> > instantiated with unsigned types.

>
> > template <typename unsigned T>
> > inline T id(T *i) {
> > return i;
> > }

>
> > void foo() {
> > (void)id<int>(k); // should return an error.
> > (void)id<unsigned int>(k); // should be fine.
> > }

>
> I don't know what algorithm you are indending to implement (your
> example is contrived and invalid).


Err, I meant

template class <typename T> // How do I reflect the signedness or
unsignedness restriction here?
inline T id(T i) {
return i;
}

It is contrived. I don't think it's important for you to see the
algorithm. I want to know how to restrict the signedness of templates
in the general case. I have found that when I post specifics about the
algorithm, people focus their comments on the algorithm, when I'm
interested in entirely something else.

> The general way is to just accept unqualified T but apply a


I didn't quite get that. Please resend.

Thanks,

Damian
  Réponse avec citation
Vieux 07/04/2008, 21h18   #4
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Restricting template parameters to signed or unsigned types, but not both

Damian wrote:
> On Apr 7, 10:10 am, Ron Natalie <r...@spamcop.net> wrote:
>> Damian wrote:
>>> Hi there,

>>
>>> I would like to restrict a template parameter to a template
>>> functioned to either a signed or unsigned type, but not both.

>>
>>> For example, the following templated function should ideally only be
>>> instantiated with unsigned types.

>>
>>> template <typename unsigned T>
>>> inline T id(T *i) {
>>> return i;
>>> }

>>
>>> void foo() {
>>> (void)id<int>(k); // should return an error.
>>> (void)id<unsigned int>(k); // should be fine.
>>> }

>>
>> I don't know what algorithm you are indending to implement (your
>> example is contrived and invalid).

>
> Err, I meant
>
> template class <typename T> // How do I reflect the signedness or
> unsignedness restriction here?
> inline T id(T i) {
> return i;
> }
>
> It is contrived. I don't think it's important for you to see the
> algorithm. I want to know how to restrict the signedness of templates
> in the general case. I have found that when I post specifics about the
> algorithm, people focus their comments on the algorithm, when I'm
> interested in entirely something else.


Often enough we see folks asking "how do I <whatever>" thinking that
it would solve some problem they allegedly have, while the actual
solution to what's holding them from an acceptable result is nothing
close. That is why sometimes, especially if the solution isn't at
all an easy one, we might need proof (to ourselves) that we're not
spending time solving the problem that you don't actually have.

There is nothing personal here, please understand.

As to your particular inquiry, you should probably look into Boost's
"enable_if" template and type traits, so you could "enable if your
type T has a trait of being of the unsigned [integral] variety".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 07/04/2008, 21h45   #5
Damian
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Restricting template parameters to signed or unsigned types, butnot both

On Apr 7, 1:18 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> Damian wrote:
> > On Apr 7, 10:10 am, Ron Natalie <r...@spamcop.net> wrote:
> >> Damian wrote:
> >>> Hi there,

>
> >>> I would like to restrict a template parameter to a template
> >>> functioned to either a signed or unsigned type, but not both.

>
> >>> For example, the following templated function should ideally only be
> >>> instantiated with unsigned types.

>
> >>> template <typename unsigned T>
> >>> inline T id(T *i) {
> >>> return i;
> >>> }

>
> >>> void foo() {
> >>> (void)id<int>(k); // should return an error.
> >>> (void)id<unsigned int>(k); // should be fine.
> >>> }

>
> >> I don't know what algorithm you are indending to implement (your
> >> example is contrived and invalid).

>
> > Err, I meant

>
> > template class <typename T> // How do I reflect the signedness or
> > unsignedness restriction here?
> > inline T id(T i) {
> > return i;
> > }

>
> > It is contrived. I don't think it's important for you to see the
> > algorithm. I want to know how to restrict the signedness of templates
> > in the general case. I have found that when I post specifics about the
> > algorithm, people focus their comments on the algorithm, when I'm
> > interested in entirely something else.

>
> Often enough we see folks asking "how do I <whatever>" thinking that
> it would solve some problem they allegedly have, while the actual
> solution to what's holding them from an acceptable result is nothing
> close. That is why sometimes, especially if the solution isn't at
> all an easy one, we might need proof (to ourselves) that we're not
> spending time solving the problem that you don't actually have.
>
> There is nothing personal here, please understand.


Not taking anything personally. I understand list traffic frequently
deals with questions about algorithms. However, while I am sure many
of this groups' members are quite skilled in algorithms, it is not
typically what I would come to a C++ list for.

Really what I am interested in how to restrict template arguments on
signedness as well as base class. It looks like boost has some
sophisticated template tools so I will look into them more deeply.
Thanks for bringing them to my attention.

> As to your particular inquiry, you should probably look into Boost's
> "enable_if" template and type traits, so you could "enable if your
> type T has a trait of being of the unsigned [integral] variety".


Thanks. I will look into enable_if construct in Boost.

Damian
  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 23h22.


É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,16072 seconds with 13 queries