Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > is there a way to Bind templates parameter lists?

Reply
Thread Tools

is there a way to Bind templates parameter lists?

 
 
ManicQin
Guest
Posts: n/a
 
      08-03-2008
I have two templated classes
for example let's say:

template <typename _T , class _Trait>
class modifier
{
public:
_T modify()
{
return _Trait::change(m_data);
}

_T m_data;
};

template <typename _T>
class TraitSample
{
static _T change(_T value) { return value + 1; }
};

when I instantiate a modifier class I need to supply the type of the
typename
in the modifier and in the trait class:
modifier<int ,TraitSample<int> > sample;
is there a way to "bind" the two parameters?
(and in the case of more parameters a way to pair them all)

Thanks.
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      08-03-2008
ManicQin wrote:

> I have two templated classes
> for example let's say:
>
> template <typename _T , class _Trait>


Names starting with an underscore followed by an uppercase letter are
reserved for the compiler. You should not use them for your own purposes.

> class modifier
> {
> public:
> _T modify()
> {
> return _Trait::change(m_data);
> }
>
> _T m_data;
> };
>
> template <typename _T>
> class TraitSample
> {
> static _T change(_T value) { return value + 1; }
> };
>
> when I instantiate a modifier class I need to supply the type of the
> typename
> in the modifier and in the trait class:
> modifier<int ,TraitSample<int> > sample;
> is there a way to "bind" the two parameters?
> (and in the case of more parameters a way to pair them all)


You can specify default types for template parameters, like:

template <typename _T , class _Trait = TraitSample<_T> >
class modifier
....

 
Reply With Quote
 
 
 
 
ManicQin
Guest
Posts: n/a
 
      08-04-2008
On Aug 3, 11:16*pm, Rolf Magnus <ramag...@t-online.de> wrote:

>
> You can specify default types for template parameters, like:
>
> template <typename _T , class _Trait = TraitSample<_T> >
> class modifier
> ...


Yes but if I want to change the TraitSample in any other class
that wont be very helpful... I'll be in the same situation.
 
Reply With Quote
 
ManicQin
Guest
Posts: n/a
 
      08-04-2008
On Aug 4, 8:34*am, ManicQin <Manic...@gmail.com> wrote:

> Yes but if I want to change the TraitSample in any other class
> that wont be very helpful... I'll be in the same situation.


I meant to any other class.

>Names starting with an underscore followed by an uppercase letter are
>reserved for the compiler. You should not use them for your own purposes.


I'm used to see STL headers using the same convention so I adopted it.
What is the preferred convention?
 
Reply With Quote
 
Francesco
Guest
Posts: n/a
 
      08-04-2008
On 4 Ago, 08:46, ManicQin <Manic...@gmail.com> wrote:
> On Aug 4, 8:34*am, ManicQin <Manic...@gmail.com> wrote:
>
> > Yes but if I want to change the TraitSample in any other class
> > that wont be very helpful... I'll be in the same situation.

>
> I meant to any other class.
>
> >Names starting with an underscore followed by an uppercase letter are
> >reserved for the compiler. You should not use them for your own purposes..

>
> I'm used to see STL headers using the same convention so I adopted it.
> What is the preferred convention?


Hi,
try something like the following.
Bye,
Francesco


template <typename _T>
class TraitSample
{
static _T change(_T value) { return value + 1; }


};

template <typename _T>
class TraitSample2
{
static _T change(_T value) { return value + 1; }


};


template <typename _T , template< typename > class _Trait =
TraitSample >
class modifier
{
public:
_T modify()
{
return _Trait< _T >::change(m_data);
}


_T m_data;

};

int main()
{
modifier< int > object;
modifier< double, TraitSample2 > object2;
}

 
Reply With Quote
 
Francesco
Guest
Posts: n/a
 
      08-04-2008
On 4 Ago, 10:28, "Alf P. Steinbach" <al...@start.no> wrote:
> * Francesco:
>
> >> * Rolf Magnus

>
> >>> Names starting with an underscore followed by an uppercase letter are
> >>> reserved for the compiler. You should not use them for your own purposes.

>
> > Hi,
> > try something like the following.
> > Bye,
> > Francesco

>
> > template <typename _T>

>
> This seems like trolling.
>
> But perhaps you didn't understand what Rolf Magnus wrote.
>
> In that case, translation: *do not* use names starting with underscore followed
> by uppercase.
>
> Cheers, & hth.,
>
> - Alf
>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?


Sorry, I just copied and paste to make an example of template template
arguments which I thought was what the OP was looking for. Obviously
Mr. Magnus comments stand. Didn't mean to do any trolling.
Regards,
F
 
Reply With Quote
 
ManicQin
Guest
Posts: n/a
 
      08-04-2008
On Aug 4, 10:28*am, "Alf P. Steinbach" <al...@start.no> wrote:

> This seems like trolling.


Sorry for starting a trolling but to who\what were you referring?

Fancesco Wrote:
>template <typename _T>
>class TraitSample
>{
> static _T change(_T value) { return value + 1; }
>
>};
>
>template <typename _T>
>class TraitSample2
>{
> static _T change(_T value) { return value + 1; }
>
>};
>
>template <typename _T , template< typename > class _Trait =
>TraitSample >
>class modifier
>{
>public:
> _T modify()
> {
> return _Trait< _T >::change(m_data);
> }
>
> _T m_data;
>
>};
>
>int main()
>{
> modifier< int > object;
> modifier< double, TraitSample2 > object2;


Thank you Francesco you were right that was the solution I was looking
for.
The problem is that when combining your advice with inheritance of
modifier
class and longer template parameter lists (slowly specializing the
modifier class)
it becomes hard to implement it (at least for a novice programmer like
me).

Thank you!
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-04-2008
On Aug 4, 8:46 am, ManicQin <Manic...@gmail.com> wrote:
> On Aug 4, 8:34 am, ManicQin <Manic...@gmail.com> wrote:


> >Names starting with an underscore followed by an uppercase
> >letter are reserved for the compiler. You should not use them
> >for your own purposes.


> I'm used to see STL headers using the same convention so I
> adopted it.


The STL is part of the implementation, so it cannot use names
that you are allowed to use. That's why such names are
reserved.

> What is the preferred convention?


Just about anything else. For container templates, a lot of
people use simply T (although I'm not sure that that's a good
idea).

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to bind a particular IP address for outbound reques ralderton ASP .Net 0 05-10-2009 04:43 AM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Is there a way to bind a simple Table or similar to... =?Utf-8?B?SmF5YnVmZmV0?= ASP .Net 5 02-23-2006 04:06 PM
Is there a way to bind a socket to a specific local interface ina client application? Joshua M. Miller Ruby 3 03-29-2005 01:48 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments