473,385 Members | 1,356 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,385 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 1548
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...

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.