|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|