Templates templates templates

J

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>();
}



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
 
A

Alf P. Steinbach

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

tom_usenet

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
 
J

JKop

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
 

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,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top