On 28/06/2006, at 2:21 PM, Jason Merrill wrote:

Geoffrey Keating wrote:

[#pragma visibility affecting explicit instantiations]
A consequence of this is that if a user instantiates a template that
they don't 'own' (that is, a template from a different module), they
must make sure that no #pragma is in effect, because the other module
may have a specific idea about what visibility should be used.

Can you give an example? You can also get different visibility implicitly, if a template argument has more restricted visibility than the template; the instantiation gets the more restricted one.

Suppose a library template has (my syntax may not be quite right):

template struct foo <class T> __attribute__((visibility("default"))) {
  static T my_var;
  T inc (T x) { return my_var += x; }
};

The intention is that all foo<T>s share the same my_var. Obviously this won't work right if a #pragma on the instantiation overrides the visibility.

I'm not sure how exactly they can do this.  Is there a visibility
pragma for "no setting"?

#pragma GCC visibility pop

... and a way to get back to what you were using before?

It seems like this would be a common enough mistake that there should
be a way to get a warning about it.

I think it would be intentional more often than it would be a mistake.

In the traditional declaration/definition model, if you try to change
the linkage of something you get an error...

Yes. If you try to change the visibility of something you also get an error. But an instantiation need not have the same visibility as its template.

For instance, code coming from VC++ will tend to default to hidden visibility, but make the interfaces default visibility. If a particular instantiation of a template is part of the interface of a non-template class, it must also get default visibility.

Right; and I'm not saying that there should be no way to achieve this, it's just that people are often unsure or unknowing of what pragmas might be in effect, so it might be better if they had to explicitly use an attribute. Certainly if someone says

template <> foo<myclass> __attribute__((visibility("hidden")));

then they ought to get what they asked for.


Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to