472,800 Members | 1,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,800 software developers and data experts.

Same template arguments, different specializations


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
1 1520
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
8
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY...
3
by: Kevin Ruland | last post by:
Hi all. I have a template class with conversion operator: template< typename T > class FooWrapper { public: FooWrapper( const T& rhs ); }
1
by: Alfonso Morra | last post by:
I have a function template declared as ff: template <class T1, class T2> size_t find( const T1& col, const T2& val, const ColFindEnum& dir ) ; I wanted to specialize it for string,long as...
9
by: Hendrik Schober | last post by:
Hi, this code #include <iostream> template< typename T > struct X { void f(); };
4
by: Alfonso Morra | last post by:
Does VC 7.1 support template specialization and partial specialization ?
5
by: Niklas Norrthon | last post by:
I've been banging my head in the wall for some time now over a little problem having to do with partial specialization of function templates. The real problem is more complex than this, but...
2
by: Joseph Turian | last post by:
I'm posting this question for a friend who lacks USENET access. He and I were discussing this question and could not figure out the solution. Thank you for your help Joseph ===
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.