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

Templates and pasring

vch
When defining templates, can I cound on the compiler to parse only those
templates that are actually used?

For example, in the following definitions:

<code>

template <class T>
struct Conv
{
// force compile error
abracadabra
};
template <>
struct Conv<int>
{
int foo();
};

template <>
struct Conv<char>
{
// force compile error
abracadabra
};

void f()
{
Conv<int>::foo();
}

</code>

Can I be sure that:

1. If somebody tries to use, say, Conv<char> or Conv<double>, they will
get compile error caused by abracadabra.
2. If they use only Conv<int>, neither generic template nor its
specialization on char will be parsed by the compiler, and no compile
errors will be generated?
Jul 23 '05 #1
6 2216
vch wrote:
When defining templates, can I cound on the compiler to parse only those
templates that are actually used?
I will parse whatever template you give it, unless it's in some #if or
#ifdef.
For example, in the following definitions:

<code>

template <class T>
struct Conv
{
// force compile error
abracadabra
};
template <>
struct Conv<int>
{
int foo();
};

template <>
struct Conv<char>
{
// force compile error
abracadabra
};

void f()
{
Conv<int>::foo();
}

</code>

Can I be sure that:

1. If somebody tries to use, say, Conv<char> or Conv<double>, they will
get compile error caused by abracadabra.
They will always get a compile error.
2. If they use only Conv<int>, neither generic template nor its
specialization on char will be parsed by the compiler, and no compile
errors will be generated?


Wrong.The code must be syntactically correct, since it will be parsed.

Jul 23 '05 #2
vch wrote:
When defining templates, can I cound on the compiler to parse only
those templates that are actually used?

For example, in the following definitions:

<code>

template <class T>
struct Conv
{
// force compile error
abracadabra
};
template <>
struct Conv<int>
{
int foo();
};

template <>
struct Conv<char>
{
// force compile error
abracadabra
};

void f()
{
Conv<int>::foo();
}

</code>

Can I be sure that:

1. If somebody tries to use, say, Conv<char> or Conv<double>, they
will get compile error caused by abracadabra.
Kind of. When the code is compiled, the template code is parsed to
determine if there are any glaring syntax errors or any other errors
that are not dependent on how the template is instantiated. However,
some errors (like the existence of a certain member in the 'T' class)
are impossible to diagnose until the template is acutally instantiated.

Those properties of the process are the basis for compile-time asserts
you can read about all over.

Remember, though, that it depends on what in particular is used from
the template. Often,
2. If they use only Conv<int>, neither generic template nor its
specialization on char will be parsed by the compiler, and no compile
errors will be generated?


Again, if the generic template and the 'char' specialisation are such
that do not have syntax errors but instead only depend on the actual
type, then sure, it can be done. Here is an example (you can find
more on the Web or in books or in the news archives):

template<class T> class A; // actually undefined
template<> class A<int> {
public:
void foo();
};

template<> class A<char> {
const int& ri; // impossible to default-initialise
};

int main() {
A<int> ai; // fine
ai.foo(); // fine
A<double> ad; // error - A is not defined for 'double'
A<char> ac; // error - A<char> cannot be constructed
}

Comeau, for example, warns about 'ri' of A<char>, but the only two
errors it reports are still the attempts to define 'ad' and 'ac'.
Same for VC++. I didn't check with others, but that should be seen
across the board.

V
Jul 23 '05 #3

vch wrote:
When defining templates, can I cound on the compiler to parse only those
templates that are actually used?

For example, in the following definitions:

<code>

template <class T>
struct Conv
{
// force compile error
abracadabra
};
template <>
struct Conv<int>
{
int foo();
};

template <>
struct Conv<char>
{
// force compile error
abracadabra
};

void f()
{
Conv<int>::foo();
}

</code>

Can I be sure that:

1. If somebody tries to use, say, Conv<char> or Conv<double>, they will
get compile error caused by abracadabra.
2. If they use only Conv<int>, neither generic template nor its
specialization on char will be parsed by the compiler, and no compile
errors will be generated?


To restate your question using more common terms: Is it possible to
restrict instantiation of a class template to only those types for
which the template has been specialized?

In the example given, Conv<int> and Conv<char> could be instantiated
(because they are specialized), while a Conv instatiated on any other
type would cause an error. (Note that the example seems to want
Conv<char> not to be able to be instantiated; if so, its specialization
Conv<char> should simply be deleted from the source code.)

One of the easiest ways to ensure that the general class template
cannot be instantiated is to declare it with a private constructor:

template <class T>
struct Conv
{
private:
Conv();
};

and then be sure that each specialization of Conv declares a public
constructor:

template <>
struct Conv<int>
{
public:
Conv();
...
};

Greg

Jul 23 '05 #4
Greg wrote:
[..]
One of the easiest ways to ensure that the general class template
cannot be instantiated is to declare it with a private constructor:

template <class T>
struct Conv
{
private:
Conv();
};


I always thought that it is actually easier just to omit the
template definition. Just declare it and move on.

V
Jul 23 '05 #5
Victor Bazarov wrote:
Greg wrote:
[..]
One of the easiest ways to ensure that the general class template
cannot be instantiated is to declare it with a private constructor:

template <class T>
struct Conv
{
private:
Conv();
};


I always thought that it is actually easier just to omit the
template definition. Just declare it and move on.

V


Omitting the declaration for the general class template entirely is
certainly more succinct. And as long as none is ever declared, will
prove just as effective as preventing instantiation as a declaration
with a hidden constructor.

Of course there's always a chance some fool, er, I mean, client will
include the header, Conv.h, note the missing declaration and say to
themselves "oh he forgot to declare the body for template class Conv,
no problem, I'll just do it for him...".

Providing the declaration in full, instead of a forward declaration,
has the slight advantage in making clear that the declaration was not
accidentally omitted while at the same time foreclosing anyone else
from providing the declaration of the class template Conv on their own.
And the fact that the client is only trying to be helpful and the rest
of the ensuing conversation - were that ever to happen - can be
blissfully avoided.

Greg

Jul 23 '05 #6
Greg wrote:
Victor Bazarov wrote:
Greg wrote:
[..]
One of the easiest ways to ensure that the general class template
cannot be instantiated is to declare it with a private constructor:

template <class T>
struct Conv
{
private:
Conv();
};
I always thought that it is actually easier just to omit the
template definition. Just declare it and move on.

V


Omitting the declaration for the general class template entirely is
certainly more succinct.


I didn't suggest omitting the declaration. Only the definition.
And as long as none is ever declared, will
prove just as effective as preventing instantiation as a declaration
with a hidden constructor.

Of course there's always a chance some fool, er, I mean, client will
include the header, Conv.h, note the missing declaration and say to
themselves "oh he forgot to declare the body for template class Conv,
no problem, I'll just do it for him...".
The body, as you put it, is actually the definition (that part between
and including the curly braces).
Providing the declaration in full, instead of a forward declaration,
has the slight advantage in making clear
That's what the comments are for.
[...]


V
Jul 23 '05 #7

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...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.