is there a way to Bind templates parameter lists?

Discussion in 'C++' started by ManicQin, Aug 3, 2008.

  1. ManicQin

    ManicQin Guest

    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.
    ManicQin, Aug 3, 2008
    #1
    1. Advertising

  2. ManicQin

    Rolf Magnus Guest

    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
    ....
    Rolf Magnus, Aug 3, 2008
    #2
    1. Advertising

  3. ManicQin

    ManicQin Guest

    On Aug 3, 11:16 pm, Rolf Magnus <> 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.
    ManicQin, Aug 4, 2008
    #3
  4. ManicQin

    ManicQin Guest

    On Aug 4, 8:34 am, ManicQin <> 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?
    ManicQin, Aug 4, 2008
    #4
  5. ManicQin

    Francesco Guest

    On 4 Ago, 08:46, ManicQin <> wrote:
    > On Aug 4, 8:34 am, ManicQin <> 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;
    }
    Francesco, Aug 4, 2008
    #5
  6. ManicQin

    Francesco Guest

    On 4 Ago, 10:28, "Alf P. Steinbach" <> 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
    Francesco, Aug 4, 2008
    #6
  7. ManicQin

    ManicQin Guest

    On Aug 4, 10:28 am, "Alf P. Steinbach" <> 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!
    ManicQin, Aug 4, 2008
    #7
  8. ManicQin

    James Kanze Guest

    On Aug 4, 8:46 am, ManicQin <> wrote:
    > On Aug 4, 8:34 am, ManicQin <> 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
    James Kanze, Aug 4, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmF5YnVmZmV0?=

    Is there a way to bind a simple Table or similar to...

    =?Utf-8?B?SmF5YnVmZmV0?=, Feb 23, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    382
    =?Utf-8?B?SmF5YnVmZmV0?=
    Feb 23, 2006
  2. JKop
    Replies:
    3
    Views:
    441
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    379
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. recover
    Replies:
    2
    Views:
    779
    recover
    Jul 25, 2006
  5. ralderton
    Replies:
    0
    Views:
    361
    ralderton
    May 10, 2009
Loading...

Share This Page