Explicit instantiation/specialization confusion

D

Dilip

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?
 
V

Victor Bazarov

Dilip said:
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
 
D

Dilip

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

isnt' it 'int'?
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?
 
V

Victor Bazarov

Dilip said:
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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top