This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: C++ 'extern inline' magic possible?


On 2 March 2011 13:00, Kevin P. Fleming wrote:
> On 03/01/2011 04:52 PM, Jonathan Wakely wrote:
>>
>> On 1 March 2011 12:14, Kevin P. Fleming wrote:
>>>
>>> Yes, that was what my original post requested, but I now understand that
>>> any
>>> solution that offered that would be non-compliant with the C++ standard.
>>> I
>>> believe this code base is only using this technique to shave some compile
>>> time for TUs that don't actually need the class definitions (they only
>>> receive and send around pointers to the classes), but it's a cause of
>>> lower
>>> performance and so I'll have to figure out whether removing the 'forward
>>> declaration only' mechanism will be worth the effort.
>>
>> There is another option, which I'm loathe to mention ...
>
> <snip macro example which makes everyone shudder <G>>
>
> Yes, I considered that. I did arrive at a potential solution, although I
> can't use it in this particular code base because of other complications,
> but...
>
> == test.h ==
> class Foo;
>
> void opaqueBar(Foo*);
>
> template <typename T>
> void Bar(T* obj)
> {
> ? ? ? ?opaqueBar(obj);
> }
>
> == test1.h ==
> #include "test.h"
>
> static void test1(Foo* obj)
> {
> ? ? ? ?Bar(obj);
> }
>
> == test2.h ==
> #include "test.h"
>
> class Foo
> {
> public:
> ? ? ? ?void realBar();
> };
>
> template <>
> inline void Bar<Foo>(Foo* obj)
>
> {
> ? ? ? ?obj->realBar();
> }
>
> static void test2(Foo* obj)
> {
> ? ? ? ?Bar(obj);
> }
>
> I think this still might be an ODR violation though, since there will end up
> being two definitions of Bar() for Foo.

Yep:

[templ.expl.spec]p6:
If a template, a member template or the member of a class template is
explicitly specialized then that
specialization shall be declared before the first use of that
specialization that would cause an implicit instantiation
to take place, in every translation unit in which such a use occurs;
no diagnostic is required.


The answer is to overload Bar as a non-template, not to specialize the
template function.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]