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 > Repeating template parameter
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Repeating template parameter

Réponse
 
LinkBack Outils de la discussion
Vieux 10/12/2007, 14h32   #1
mathieu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Repeating template parameter

Hi there,

I guess this is a pretty vague question but I stumble on it a couple
of times and never really knew what was a nice solution to it. I am
trying to avoid passing twice a template parameter when it could be
either deduced or simple reused. For instance I have a calculator
class that take a simple er function that perform an operation on
the same type as my calculator,.
I would write something like this:

template <typename T>
struct er1
{
inline double operator() (T t) { return t; }
};
template <typename T>
struct er2
{
inline double operator() (T t) { return -t; }
};

template <typename T, typename Ter>
struct Calculator
{
static double compute(double t) {
Ter th;
return th(t);
}
};

int main()
{
Calculator<double, er1<double> > c;
return 0;
}

Notice how I write twice 'double' in

Calculator<double, er1<double> > c;

Thanks for suggestion,
-Mathieu
  Réponse avec citation
Vieux 10/12/2007, 15h24   #2
Ondra Holub
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Repeating template parameter

On 10 Pro, 15:32, mathieu <mathieu.malate...@gmail.com> wrote:
> Hi there,
>
> I guess this is a pretty vague question but I stumble on it a couple
> of times and never really knew what was a nice solution to it. I am
> trying to avoid passing twice a template parameter when it could be
> either deduced or simple reused. For instance I have a calculator
> class that take a simple er function that perform an operation on
> the same type as my calculator,.
> I would write something like this:
>
> template <typename T>
> struct er1
> {
> inline double operator() (T t) { return t; }};
>
> template <typename T>
> struct er2
> {
> inline double operator() (T t) { return -t; }
>
> };
>
> template <typename T, typename Ter>
> struct Calculator
> {
> static double compute(double t) {
> Ter th;
> return th(t);
> }
>
> };
>
> int main()
> {
> Calculator<double, er1<double> > c;
> return 0;
>
> }
>
> Notice how I write twice 'double' in
>
> Calculator<double, er1<double> > c;
>
> Thanks for suggestion,
> -Mathieu


Try this:

template<
typename T,
template<typename T> class Ter
>

struct Calculator
  Réponse avec citation
Vieux 10/12/2007, 15h43   #3
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Repeating template parameter

Ondra Holub wrote:
> On 10 Pro, 15:32, mathieu <mathieu.malate...@gmail.com> wrote:
>> Hi there,
>>
>> I guess this is a pretty vague question but I stumble on it a
>> couple of times and never really knew what was a nice solution to
>> it. I am trying to avoid passing twice a template parameter when it
>> could be either deduced or simple reused. For instance I have a
>> calculator class that take a simple er function that perform an
>> operation on the same type as my calculator,.
>> I would write something like this:
>>
>> template <typename T>
>> struct er1
>> {
>> inline double operator() (T t) { return t; }};
>>
>> template <typename T>
>> struct er2
>> {
>> inline double operator() (T t) { return -t; }
>>
>> };
>>
>> template <typename T, typename Ter>
>> struct Calculator
>> {
>> static double compute(double t) {
>> Ter th;
>> return th(t);
>> }
>>
>> };
>>
>> int main()
>> {
>> Calculator<double, er1<double> > c;
>> return 0;
>>
>> }
>>
>> Notice how I write twice 'double' in
>>
>> Calculator<double, er1<double> > c;
>>
>> Thanks for suggestion,
>> -Mathieu

>
> Try this:
>
> template<
> typename T,
> template<typename T> class Ter
>>

> struct Calculator


In order to instantiate the local variable 'th' in the member 'compute'
of 'Calculator', the second argument (as currently written) has to be
a concrete class, it cannot be a template. With your suggestion, the
'Calculator::compute' would have to be rewritten as

static double compute(double t) {
Ter<T> th;
return th(t);
}

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 10/12/2007, 15h50   #4
Daniel Lidstrom
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Repeating template parameter

On 10 Dec, 15:32, mathieu <mathieu.malate...@gmail.com> wrote:
> template <typename T, typename Ter>
> struct Calculator
> {
> static double compute(double t) {
> Ter th;
> return th(t);
> }
>
> };
>
> int main()
> {
> Calculator<double, er1<double> > c;
> return 0;
>
> }
>
> Notice how I write twice 'double' in
>
> Calculator<double, er1<double> > c;


If you're using a modern C++ compiler (VC7.1 or newer, gcc 3.4, etc)
you can use template template parameters. Here's what it will look
like:

template <typename T, template<typename> class Ter>
struct Calculator
{
static double compute(double t) {
Ter<T> th;
return th(t);
}

};

int main()
{
Calculator<double, er1> c;
double t = c.compute(5);
return 0;
}


Hope this s!

--
Daniel
  Réponse avec citation
Vieux 10/12/2007, 15h58   #5
Abhishek Padmanabh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Repeating template parameter

On Dec 10, 7:32 pm, mathieu <mathieu.malate...@gmail.com> wrote:
> Hi there,
>
> I guess this is a pretty vague question but I stumble on it a couple
> of times and never really knew what was a nice solution to it. I am
> trying to avoid passing twice a template parameter when it could be
> either deduced or simple reused. For instance I have a calculator
> class that take a simple er function that perform an operation on
> the same type as my calculator,.
> I would write something like this:
>
> template <typename T>
> struct er1
> {
> inline double operator() (T t) { return t; }};
>
> template <typename T>
> struct er2
> {
> inline double operator() (T t) { return -t; }
>
> };
>
> template <typename T, typename Ter>
> struct Calculator
> {
> static double compute(double t) {
> Ter th;
> return th(t);
> }
>
> };
>
> int main()
> {
> Calculator<double, er1<double> > c;
> return 0;
>
> }
>
> Notice how I write twice 'double' in
>
> Calculator<double, er1<double> > c;
>


You can make Ter as a template template parameter to the
calculator template as below:

template <typename T, template<typename> class Ter>
struct Calculator
{
static double compute(double t) {
Ter<T> th;
return th(t);
}
};
  Réponse avec citation
Vieux 10/12/2007, 17h11   #6
mathieu
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Repeating template parameter

On Dec 10, 4:58 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
> On Dec 10, 7:32 pm, mathieu <mathieu.malate...@gmail.com> wrote:
>
>
>
> > Hi there,

>
> > I guess this is a pretty vague question but I stumble on it a couple
> > of times and never really knew what was a nice solution to it. I am
> > trying to avoid passing twice a template parameter when it could be
> > either deduced or simple reused. For instance I have a calculator
> > class that take a simple er function that perform an operation on
> > the same type as my calculator,.
> > I would write something like this:

>
> > template <typename T>
> > struct er1
> > {
> > inline double operator() (T t) { return t; }};

>
> > template <typename T>
> > struct er2
> > {
> > inline double operator() (T t) { return -t; }

>
> > };

>
> > template <typename T, typename Ter>
> > struct Calculator
> > {
> > static double compute(double t) {
> > Ter th;
> > return th(t);
> > }

>
> > };

>
> > int main()
> > {
> > Calculator<double, er1<double> > c;
> > return 0;

>
> > }

>
> > Notice how I write twice 'double' in

>
> > Calculator<double, er1<double> > c;

>
> You can make Ter as a template template parameter to the
> calculator template as below:
>
> template <typename T, template<typename> class Ter>
> struct Calculator
> {
> static double compute(double t) {
> Ter<T> th;
> return th(t);
> }
>
> };


Thanks ! I never used template template parameter before

pretty cool indeed !

-Mathieu
  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 12h31.


É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,17716 seconds with 14 queries