473,465 Members | 1,366 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Classes declared in templated functions

Hi all,

If a class is declared inside a templated function, e.g.
template <typenamefn()
{
class A {};
static A a;
}
And I instantiate fn with the same typename twice, in two different
compilation units,

----
// a.cpp
void b();

int main()
{
fn<int>();
b();
}

----
// b.cpp

void b()
{
fn<int>(); // identical instantiation
}

Will the static member a be multiply defined at link time? I would think not,
as the two template instantiations have identical instantiation parameters,
and thus should correspond to a single instance. However, MSVC 2005 seems to
think otherwise.

In addition, I would guess that a /different/ instantiation of fn (e.g.
fn<double>()) will yield a /different/ instance of a. Am I right?

-dr
Mar 3 '07 #1
1 1316
Dave Rahardja wrote:
Hi all,

If a class is declared inside a templated function, e.g.
template <typenamefn()
{
class A {};
static A a;
}
And I instantiate fn with the same typename twice, in two different
compilation units,

----
// a.cpp
void b();

int main()
{
fn<int>();
b();
}

----
// b.cpp

void b()
{
fn<int>(); // identical instantiation
}

Will the static member a be multiply defined at link time? I would think not,
as the two template instantiations have identical instantiation parameters,
and thus should correspond to a single instance. However, MSVC 2005 seems to
think otherwise.
I would say you are right, however I can't reproduce the problem you
claim with MSVC 2005.
>
In addition, I would guess that a /different/ instantiation of fn (e.g.
fn<double>()) will yield a /different/ instance of a. Am I right?
I think you're right again.
>
-dr
john
Mar 3 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
5
by: Shlomi | last post by:
Hi ! I've wrote quite a big code which bases on a considerable large amount of template classes. (uses many different kinds of each template class). Now, without prior knowledge of mine, it...
2
by: Thomas Matthews | last post by:
Hi, I would like to create a table (or vector) of pointers to templated functions. 1. How do I declare a typedef of a pointer to a templated function? For example, I have some functions...
21
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
19
by: dl | last post by:
I'll try to clarify the cryptic subject line. Let's say I have a base class 'GeometricObject' with a virtual method 'double distance (const GeometricObject &) const'. Among the derived classes...
12
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
6
by: aniait | last post by:
I have declared and defined the class LinkedList as: template <class T> class LinkedList { private: class List { public: List(T& aEle); public: T& iEle; List* iNext; }; List* iHeader; ...
2
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
1
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.