Group: comp.lang.c++
From: "Victor Bazarov"
Date: Monday, April 07, 2008 3:18 PM
Subject: Re: Restricting template parameters to signed or unsigned types, but not both

Damian wrote:
> On Apr 7, 10:10 am, Ron Natalie 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
>>> inline T id(T *i) {
>>> return i;
>>> }
>>
>>> void foo() {
>>> (void)id(k); // should return an error.
>>> (void)id(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 // 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 " 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


Safety Articles | Usenet Groups | Usenet News | Bluegrass