473,549 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1565
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
2006
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 instantiation of foo:
8
11347
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 f2(ANY, ANY) ANY f3(ANY, ANY, ANY)
3
1380
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
1169
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 ff: template<> //string, long specialization size_t find( const std::string& col, long& val, const ColFindEnum& dir )
9
1582
by: Hendrik Schober | last post by:
Hi, this code #include <iostream> template< typename T > struct X { void f(); };
4
1843
by: Alfonso Morra | last post by:
Does VC 7.1 support template specialization and partial specialization ?
5
2242
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 after trimming out irrelevant stuff, this is what remains. The problem is to have a function template with different specialization for signed an...
2
2185
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
13939
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 dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
0
7464
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7979
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7497
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6065
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5107
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3512
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1074
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
781
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.