last day (15 days later) » 

8:05 PM
9
A: What's the right way to specialize a template when using "extern template"?

ascheplerextern template class A<long>; This line says that A<long> is to be explicitly instantiated according to the definitions the compiler has already seen. When you add a specialization later, you break that meaning. Add a declaration of your specialization to the header file. template <typename T>

 
Excellent answer, but could you add whether it is legal to combine lines 2 and 4 of your example into the single line extern template<> int A<long>::get() const;? That's the question I came here in hopes of answering.
 
@Quuxplusone: Not in that syntax. If you want an explicit specialization (which you must define), use the template<> int A<long>:get() const; version. If you want an explicit instantiation (defined via the template), don't put any brackets after template: template int A<long>::get() const; (add extern in front to make it not a definition).
 
What if I want an explicit extern specialization, but want to express that concept in one single declaration? Impossible?
 
@Quuxplusone: I don't think I understand. Probably just ask a new question.
 
@aschepler I'll just edit your answer, and then you can change it if I'm wrong. ;)
 
8:05 PM
@Quuxplusone: Your example is legal, but the first line doesn't do anything. Why do you want to declare an explicit specialization, which is normally used for a definition different from the template, when you're just going to use the original template anyway?
 
Moved to chat. ;)
Like the OP, I want to define a specialization of A<T>::get() [with T=long].
But I don't want to define it right here in the header file; I want it to be defined elsewhere, in a separate translation unit.
As you pointed out, if I just tell the compiler "extern" (i.e. "you can find this instantiation elsewhere", it is allowed to ignore it and assume that it's safe to inline the "default" templated implementation.
 
A<long>::get() is called a "specialization" no matter how it's defined. You want it to use the primary A<T>, or write a different version?
 
Whereas, if I just tell the compiler "template<>" (i.e., "please don't use the default implementation, I want to specialize it"), then the compiler will complain that it can't find my specialization in this translation unit.
(Write a different version, of course.)
 
So you put template <> A<long>::get(); in the header file.
It already has external linkage and is already obviously not a definition.
 
It doesn't have external linkage if A<T>::get() has inline linkage
does it?
and A<T>::get() will have inline linkage, in many cases (such as the OP's example code)
 
8:10 PM
Yes. The inline keyword does nothing to linkage type, only to whether or not it can appear in multiple translation units. And it does nothing at all to function templates.
There is no such thing as "inline linkage".
 
Let me experiment a little bit. It was definitely inlining the wrong definition in my earlier experiments with GCC.
(I mean, the extern keyword was needed or else bad things would happen)
 
Oh, I was wrong about the example: it's the second of those two lines that has no effect. 14.7.2/5: "For a given set of template arguments, if an explicit instantiation of a template appears after a declaration of an explicit specialization for that template, the explicit instantiation has no effect."
 
Hm, well, I haven't been able to reproduce the bad behavior I was seeing in my real code. It looks like there's no way to get the specialization to be inline, but I can get it to be out-of-line.
I'll paste my test code.
test.cc:
#include <stdio.h>

template<class T> inline const char* foo() { return "default"; }

template<> const char* foo<int>();
extern template const char* foo<int>();

int main() {
printf("%s\n", foo<int>());
}
test2.cc:
template<class T> inline const char* foo();

template<>
const char* foo<int>()
{
return "int";
}
Adding inline to the declaration of foo<int> in test2.cc results in linker errors.
Which I guess makes a certain amount of sense.
 
Hmm.
 
8:26 PM
To clarify: I think this all actually agrees with what you said, i.e., I may be wrong that there's anything "wrong" here. But it's still confusing as heck to me, as to what is required and what is optional and what is invalid.
 
Ok, yeah. 14.7.3/12: "An explicit specialization of a function template is inline only if it is declared with the inline specifier or defined as deleted, and independently of whether its function template is inline.
So if the explicit specialization is marked inline, it needs to be in the same TU where it's used, and can be in multiple TUs (presumably via a header file), just like plain functions.
And your code will mean exactly the same thing if you get rid of the extern template const char* foo<int>();
 
hm, okay. So then I guess the question relevant to the OP would be "why do you need extern template class A<long> in your example?" Shouldn't your code also mean the same thing if you get rid of that line? or, what does that line add?
The OP's example is strictly more complicated than my example, because he's dealing with a member function of a class template instead of just a plain old function template.
 
8:42 PM
The program is valid if you get rid of that line, yes. What it does is promise the compiler that in some TU you have the definition template class A<long>; to instantiate the template - so if it wants, it can just link to those symbols rather than do all the implicit instantiations of A<long> members.
 
OK. So, if I understand correctly, template<> foo bar<int> says "I have a specialization of this, maybe here, maybe elsewhere; please don't use the default."; template foo bar<int> says "Please explicitly instantiate the default right here."; and extern template foo bar<int> says "I have explicitly instantiated the default somewhere else."
is that a close-enough summary of the rules?
and, you should only ever have one of those three lines, for any given declaration, in any given translation unit.
I had been thinking that sometimes two of them were needed to get the semantics I wanted.
 
9:20 PM
Re-edited your answer to remove my misinformation and hopefully now be correct. Thank you, btw. :)
 
10:07 PM
That all looks right. Except I would probably have the extern template foo bar<int>; from a header file included in the same TU that provides the definition template foo bar<int>;
 

last day (15 days later) »