473,785 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Explicit instantiation/specialization confusion


I am stumbling my way through C++ templates and I keep running into way
too many questions. Here is one:

If a library writer exposes a function template like so:

template<typena me T>
void some_func(T const& x)
{
}

Lets say the client code explicitly instantiates it:

template void some_func(int);

Later if the library writer releases a new version that explicitly
specializes this template for the same data type, what happens to the
client code? The book I am reading says this issue is still pending
with C++ committee. Has there been any decisions taken?

Or am I asking the question backwards? maybe client code is never
allowed to explicitly instantiate function/class templates from a
library?

Aug 15 '06 #1
3 2443
Dilip wrote:
I am stumbling my way through C++ templates and I keep running into
way too many questions. Here is one:

If a library writer exposes a function template like so:

template<typena me T>
void some_func(T const& x)
{
}

Lets say the client code explicitly instantiates it:

template void some_func(int);
I don't think it's a match. What's "T" in this case?
Later if the library writer releases a new version that explicitly
specializes this template for the same data type, what happens to the
client code? The book I am reading says this issue is still pending
with C++ committee. Has there been any decisions taken?
You need to ask in 'comp.std.c++' about any Standard issues.
Or am I asking the question backwards? maybe client code is never
allowed to explicitly instantiate function/class templates from a
library?
The explicit instantiation makes sure that the code is created. Where
it comes from is the user's responsibility. Generally, if the instantiation
appears in the header, all TUs that include that header participate equally
in the contest to hold the actual generated code, and the linker possesses
the ablity to resolve that. Also, all instantiations have to be the same,
otherwise ODR is violated, and I don't know if nowadays the linkers actually
care to compare the instantiations. Usually the first one is picked. Which
of the several becomes "the first one" is undefined.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 15 '06 #2
Victor Bazarov wrote:
Dilip wrote:
I am stumbling my way through C++ templates and I keep running into
way too many questions. Here is one:

If a library writer exposes a function template like so:

template<typena me T>
void some_func(T const& x)
{
}

Lets say the client code explicitly instantiates it:

template void some_func(int);

I don't think it's a match. What's "T" in this case?
isnt' it 'int'?
Or am I asking the question backwards? maybe client code is never
allowed to explicitly instantiate function/class templates from a
library?

The explicit instantiation makes sure that the code is created. Where
it comes from is the user's responsibility.
Understood. I think I didn't phrase my question properly. Assume for
the function template above, I have the following instantiation in one
of my translation units in the client code:

template void some_func(some_ cool_class);

Some days later the library writer releases a new version that includes
explicit specialization for the same function template with the same
some_cool_class by some nasty quirk of fate:

template<>
void some_func(some_ cool_class const& x)
{
}

Now if I link with this new version, am I in boom-land?

Aug 16 '06 #3
Dilip wrote:
Victor Bazarov wrote:
>Dilip wrote:
>>I am stumbling my way through C++ templates and I keep running into
way too many questions. Here is one:

If a library writer exposes a function template like so:

template<type name T>
void some_func(T const& x)
{
}

Lets say the client code explicitly instantiates it:

template void some_func(int);

I don't think it's a match. What's "T" in this case?

isnt' it 'int'?
No. The argument is a *reference* to const T. What in 'int' is a ref
to what? Nothing. That's why T cannot be figured out.
>>Or am I asking the question backwards? maybe client code is never
allowed to explicitly instantiate function/class templates from a
library?

The explicit instantiation makes sure that the code is created.
Where it comes from is the user's responsibility.

Understood. I think I didn't phrase my question properly. Assume for
the function template above, I have the following instantiation in one
of my translation units in the client code:

template void some_func(some_ cool_class);

Some days later the library writer releases a new version that
includes explicit specialization for the same function template with
the same some_cool_class by some nasty quirk of fate:

template<>
void some_func(some_ cool_class const& x)
{
}

Now if I link with this new version, am I in boom-land?
Unknown. Probably. You might want to carefully read the "release notes"
of that library to know what's changed.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 16 '06 #4

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

Similar topics

4
1844
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it elaborate to make sure my questions are clear enough. Let's say I need to write a function whose logic is same for all types (T) except in the case of T * (including const T *). Furtheremore , the function needs to be written differently for...
5
2241
by: Andy | last post by:
Hi, I got a little confused on 'instantiation' and 'specialization', espcially for explicit instantiation and explicit sepcialization. Can anybody explain the difference? Thanks a lot! Andy
6
5678
by: Vyacheslav Lanovets | last post by:
Hello, All! I know that Explicit Instantiation actually emits code to obj files (so you can even export them from the module as plain functions or classes). But I found that MSVC7.1 compiler does the same in case of Explicit Specialization, so I either have to delcare specializations inline or move definitions to cpp file to avoid LNK2005 ("already defined") errors. Why is that?
8
4803
by: Ferdi Smit | last post by:
I've never understood the rationale of allowing partial, but not explicit specialization for classes at non-namespace scope. Ie.: struct A { template <typename T1, typename T2> struct B {}; // this is not allowed: template <> struct B<int, float> {};
5
2661
by: henkoo | last post by:
i want to explicit instantiate a member function of a class template, and got some error, can anyone give some advice to correct it? 3x complier: g++ 3.2 #include <iostream> #include <string> using namespace std;
3
4008
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something handed down from an ancestor or a predecessor or from the past: a legacy of religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF, from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
5
5196
by: dascandy | last post by:
The following program produces no warnings, no errors, but doesn't do what I expect it to. I expect it to produce a warning or error. What does the standard say about this and/or what should it say? Stroustrup mentions it's an error (The C++ programming language, p343) but doesn't say whether the standard says so or whether it's implementation defined. Both GCC and MSVC return "4 4 40", showing they instantiated the template at the...
2
7699
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
0
2926
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit instantiation of the partially specialized version, but my compiler (VC8) complains because it fails the SFINAE version. I just realized as I was typing this that I'm using partial _function_ specialization. I'm sure I remember reading somewhere that
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10147
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10090
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5380
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.