473,325 Members | 2,785 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,325 software developers and data experts.

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<typename 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 2417
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<typename 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<typename 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<typename 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
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...
5
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! ...
6
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...
8
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 {}; ...
5
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...
3
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...
5
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...
2
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
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.