Same template arguments, different specializations

I

Imre

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
 
C

Chris Jefferson

Imre said:
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
 

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,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top