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