473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

is there a way to Bind templates parameter lists?

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.
Aug 3 '08 #1
6 1626
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
....

Aug 3 '08 #2
On Aug 3, 11:16*pm, Rolf Magnus <ramag...@t-online.dewrote:
>
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.
Aug 4 '08 #3
On Aug 4, 8:34*am, ManicQin <Manic...@gmail.comwrote:
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?
Aug 4 '08 #4
On 4 Ago, 08:46, ManicQin <Manic...@gmail.comwrote:
On Aug 4, 8:34*am, ManicQin <Manic...@gmail.comwrote:
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;
}

Aug 4 '08 #5
On Aug 4, 10:28*am, "Alf P. Steinbach" <al...@start.nowrote:
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!
Aug 4 '08 #6
On Aug 4, 8:46 am, ManicQin <Manic...@gmail.comwrote:
On Aug 4, 8:34 am, ManicQin <Manic...@gmail.comwrote:
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:ja*********@gmail.com
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
Aug 4 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daryl | last post by:
Hello I am using apply-templates and would like to pass a parameter to the template using with-param. Using call-template passes the parameter, but when I use apply-templates, the parameter seems...
4
by: Arturo Cuebas | last post by:
The program below contains a compile error. Following the program you will find the typical fix then my idea for a library that facilitates a more elegant fix. #include <boost\bind.hpp> using...
1
by: Simon White | last post by:
I've just upgraded to gcc 3.4 resulting in problems in some template code I have. The code shown below is a simplified version of what we are using to wrap the creation of singletons. #include...
3
by: John Crowley | last post by:
I've run into a problem where drop down lists are refusing to data bind with the following error on the DataBind() call Specified argument was out of the range of valid values. Parameter name:...
0
by: teclioness | last post by:
Hi, I am using gridview for the user to update rows. In a row, there sre two columns, which need to be updated. When the gridview is to be shown, the row should show the values from database....
4
by: RThaden | last post by:
Hi all, I looked in several books, articles, etc. but did not find a solution to my problem. Maybe somebody out there can help a desperate, not toooo experienced programmer: I want to...
0
by: Mark Micallef | last post by:
Hi, I'm having a problem using a databound dropdownlist inside a reorderlist ajax control. Here's a snippet of the code I'm using: <InsertItemTemplate> <div class="insertArea">...
1
by: Mark Micallef | last post by:
Hi, this question relates to a control in the Ajax toolkit for asp.net 2. I'm having a problem using a databound dropdownlist inside a reorderlist ajax control. Here's a snippet of the code I'm...
0
by: =?Utf-8?B?R3V5?= | last post by:
I would like developing an application for sending e-mail based on a (html) template. Scenario: user chooses distribution list and chooses a template (fro ma list of available html-templates)....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.