473,406 Members | 2,352 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,406 software developers and data experts.

Templates templates templates

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
Jul 22 '05 #1
3 1038
* 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?
Jul 22 '05 #2
On Tue, 20 Jul 2004 20:42:47 GMT, JKop <NU**@NULL.NULL> 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
Jul 22 '05 #3
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
Jul 22 '05 #4

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

Similar topics

1
by: Vince C. | last post by:
Hi all, I've created XML documents that are described with a schema. I'm using those documents to create web pages. All my web pages contain a fixed header and a variable document part. The...
5
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element...
22
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
12
by: Fabio De Francesco | last post by:
Hello. I can't understand why I can't compile the following simple code, where I think I have applied all the needed rules for templates that are declared and defined in different files (*.h and...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
2
by: jimbo_vr5 | last post by:
Hey I think i've figured out the idea behind apply-templates. But going through the tutorial on <http://www.w3schools.com/xsl/xsl_apply_templates.asp> theres simply just something that i dont...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
7
by: Chris | last post by:
Hi All, This is a weird one but I am hoping someone can help or has some pointers, a recipe how to do the following: I have to move some code from c++ to objective-c and to do this I must...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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
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.