Templates templates templates

Discussion in 'C++' started by JKop, Jul 20, 2004.

  1. JKop

    JKop Guest

    The following compiles:

    #include <iostream>
    using std::cout;
    using std::endl;

    template< class T >
    struct DisplayFormat
    {
    unsigned char radix;
    const T* digits;
    T decimal_point_symbol;
    unsigned char digits_after_decimal_point;
    T comma_symbol;
    unsigned char digits_until_comma;
    };


    DisplayFormat<wchar_t> us_display_format =
    { 10 , L"0123456789" , L'.' , 2 , L',' , 3 };


    template < DisplayFormat < wchar_t > * df> //THIS LINE
    inline void Blah()
    {
    cout << df->digits << endl;
    }



    int main()
    {
    Blah<&us_display_format>();
    }



    Where I'm having trouble is "THIS LINE". How do I turn:

    template < DisplayFormat < wchar_t > * df>

    into:


    template < template < class T > DisplayFormat < T > * df >


    Obviously I've tried the above, but it won't compile.


    -JKop
    JKop, Jul 20, 2004
    #1
    1. Advertising

  2. * JKop:
    > The following compiles:
    >
    > #include <iostream>
    > using std::cout;
    > using std::endl;
    >
    > template< class T >
    > struct DisplayFormat
    > {
    > unsigned char radix;
    > const T* digits;
    > T decimal_point_symbol;
    > unsigned char digits_after_decimal_point;
    > T comma_symbol;
    > unsigned char digits_until_comma;
    > };
    >
    > DisplayFormat<wchar_t> us_display_format =
    > { 10 , L"0123456789" , L'.' , 2 , L',' , 3 };
    >
    > template < DisplayFormat < wchar_t > * df> //THIS LINE
    > inline void Blah()
    > {
    > cout << df->digits << endl;
    > }
    >
    > int main()
    > {
    > Blah<&us_display_format>();
    > }
    >
    > How do I turn:
    >
    > template < DisplayFormat < wchar_t > * df>
    >
    > into:
    >
    > template < template < class T > DisplayFormat < T > * df >
    >
    > Obviously I've tried the above, but it won't compile.



    I don't quite understand what you're asking, but the code doesn't work.

    Here's a bit that when substituted in the above code, works:


    template< typename CharType >
    std::basic_ostream<CharType>& stdOutput();

    template<>
    std::basic_ostream<char>& stdOutput<char>(){ return std::cout; }

    template<>
    std::basic_ostream<wchar_t>& stdOutput<wchar_t>(){ return std::wcout; }

    template < typename CharType, DisplayFormat<CharType> const * df>
    inline void Blah()
    {
    stdOutput<CharType>() << df->digits << endl;
    }


    Also the call in 'main' must be adjusted (extra template argument).

    --
    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?
    Alf P. Steinbach, Jul 20, 2004
    #2
    1. Advertising

  3. JKop

    tom_usenet Guest

    On Tue, 20 Jul 2004 20:42:47 GMT, JKop <> wrote:

    >The following compiles:
    >
    >#include <iostream>
    >using std::cout;
    >using std::endl;
    >
    >template< class T >
    >struct DisplayFormat
    >{
    > unsigned char radix;
    > const T* digits;
    > T decimal_point_symbol;
    > unsigned char digits_after_decimal_point;
    > T comma_symbol;
    > unsigned char digits_until_comma;
    >};
    >
    >
    >DisplayFormat<wchar_t> us_display_format =
    > { 10 , L"0123456789" , L'.' , 2 , L',' , 3 };
    >
    >
    >template < DisplayFormat < wchar_t > * df> //THIS LINE
    >inline void Blah()
    >{
    > cout << df->digits << endl;
    >}
    >
    >
    >
    >int main()
    >{
    > Blah<&us_display_format>();
    >}
    >
    >
    >
    >Where I'm having trouble is "THIS LINE". How do I turn:
    >
    >template < DisplayFormat < wchar_t > * df>
    >
    >into:
    >
    >
    >template < template < class T > DisplayFormat < T > * df >


    You can do it with two template parameters:

    template <class CharT, DisplayFormat<CharT>* df>

    and then call it with:

    Blah<DisplayFormat<wchar_t>, &us_display_format>();

    But why don't you just pass the display format to Blah as a function
    parameter rather than a template parameter?

    Tom
    tom_usenet, Jul 21, 2004
    #3
  4. JKop

    JKop Guest

    tom_usenet posted:

    > You can do it with two template parameters:
    >
    > template <class CharT, DisplayFormat<CharT>* df>
    >
    > and then call it with:
    >
    > Blah<DisplayFormat<wchar_t>, &us_display_format>();



    That crossed my mind, but I was hoping I wouldn't have to do that.


    > But why don't you just pass the display format to Blah as a function
    > parameter rather than a template parameter?


    That was just a contrived example. DisplayFormat<T> is actually going to be
    a member of a class.


    Thanks for the input,
    -JKop
    JKop, Jul 21, 2004
    #4
    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. Fred
    Replies:
    1
    Views:
    572
    Neredbojias
    Sep 26, 2005
  2. John Harrison

    using templates in templates

    John Harrison, Jul 31, 2003, in forum: C++
    Replies:
    8
    Views:
    364
    Torsten Curdt
    Jul 31, 2003
  3. Tom McCallum

    Templates within templates

    Tom McCallum, Aug 4, 2004, in forum: C++
    Replies:
    2
    Views:
    350
    tom_usenet
    Aug 4, 2004
  4. Audrey Pratt
    Replies:
    0
    Views:
    3,218
    Audrey Pratt
    Dec 14, 2005
  5. recover
    Replies:
    2
    Views:
    778
    recover
    Jul 25, 2006
Loading...

Share This Page