By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 1,882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

Same template arguments, different specializations

P: n/a

Let's suppose we have a primary template with one argument defined in a
header file. Two source files include this header, and both define a
specialization of the primary template. Later, both sources reference
the template, using the same template argument, that matches both
specializations. Now, in theory, both sources reference the same
template with the same argument, but due to the different
specializations in the sources, the exact meaning of these seemingly
identical references should be different.

For example:

// --- MyTemplate.h ---

template <int i>
struct MyTemplate
{
enum { value = -1 };
};

// --- One.cpp ---

#include "MyTemplate.h"

template <>
struct MyTemplate<0>
{
enum { value = 1 };
};

int i1 = MyTemplate<0>::value;

// --- One.cpp ---

#include "MyTemplate.h"

template <>
struct MyTemplate<0>
{
enum { value = 2 };
};

int i2 = MyTemplate<0>::value;

Now i1 should be 1, and i2 should be 2.

My questions are:

1. According to the standard, should this work?
2. Is this likely to work on compilers that use a template repository?

Imre

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Imre wrote:
Let's suppose we have a primary template with one argument defined in a
header file. Two source files include this header, and both define a
specialization of the primary template. Later, both sources reference
the template, using the same template argument, that matches both
specializations. Now, in theory, both sources reference the same
template with the same argument, but due to the different
specializations in the sources, the exact meaning of these seemingly
identical references should be different.
<snip>

My questions are:

1. According to the standard, should this work?


My first answer would be "no", but the relevant bit of the standard is
3.2,5 which states that:

There can be more than one definition of a class type (clause 9),
enumeration type (7.2), inline function with external linkage (7.1.2),
class template (clause 14), nonstatic function template (14.5.5), static
data member of a class template (14.5.1.3), member function template
(14.5.1.1), or template specialization for which some template
parameters are not specified (14.7, 14.5.4) in a program provided that
each definition appears in a different translation unit, and provided
the definitions satisfy the following requirements.

Where the requirements basically say "it's the same definition".

Interestingly, this list doesn't contain "a template specialization for
which all template parameters are specified"

Chris
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.