is there a way to Bind templates parameter lists?

M

ManicQin

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.
 
R

Rolf Magnus

ManicQin said:
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
....
 
M

ManicQin

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.
 
M

ManicQin

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?
 
F

Francesco

I meant to any other class.


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;
}
 
F

Francesco

* Francesco:




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
 
M

ManicQin

This seems like trolling.

Sorry for starting a trolling but to who\what were you referring?
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!
 
J

James Kanze

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top