public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/56328] New: Explicit instantiation of inline function template works for base template, fails for specialized template
@ 2013-02-14 16:31 michael.schlottke at sloede dot com
  2013-02-14 18:45 ` [Bug c++/56328] " pinskia at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: michael.schlottke at sloede dot com @ 2013-02-14 16:31 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56328

             Bug #: 56328
           Summary: Explicit instantiation of inline function template
                    works for base template, fails for specialized
                    template
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: michael.schlottke@sloede.com


Why does the compiler treat a specialized template differently from the base
template when it comes to explicit instantiation of inline function templates?
The following code gives a linker error, unless the second "inline" (see AAA)
is removed. In any case, this seems to be a problem of the compiler, not the
linker.



main.cpp
====================================
template <int dim> void f(int src);

int main(int, char** )
{
  f<1>(456);
  f<2>(789);
}
====================================

template.cpp
====================================
template <int dim> void f(int src);

template <int dim> inline
void f(int src)    { (void)0; }

template <> inline /* AAA */
void f<2>(int src) { (void)0; }

template void f<1>(int);
template void f<2>(int);
====================================



Command used:
/pds/opt/gcc-4.7-2/bin/g++ main.cpp template.cpp



Error message:
/tmp/cc42UAxE.o: In function `main':
main.cpp:(.text+0x1f): undefined reference to `void f<2>(int)'
collect2: error: ld returned 1 exit status



g++ -v:
Using built-in specs.
COLLECT_GCC=/pds/opt/gcc-4.7-2/bin/g++
COLLECT_LTO_WRAPPER=/aia/opt/gcc-4.7-2/bin/../libexec/gcc/x86_64-unknown-linux-gnu/4.7.2/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /pds/opt/install/gcc//gcc-4.7.2/configure
--prefix=/pds/opt/gcc-4.7-2 --enable-threads=posix
Thread model: posix
gcc version 4.7.2 (GCC)


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/56328] Explicit instantiation of inline function template works for base template, fails for specialized template
  2013-02-14 16:31 [Bug c++/56328] New: Explicit instantiation of inline function template works for base template, fails for specialized template michael.schlottke at sloede dot com
@ 2013-02-14 18:45 ` pinskia at gcc dot gnu.org
  2013-02-14 20:40 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2013-02-14 18:45 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56328

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> 2013-02-14 18:44:41 UTC ---
(In reply to comment #0)
> Why does the compiler treat a specialized template differently from the base
> template when it comes to explicit instantiation of inline function templates?

Simple answer, inline functions have to appear in every TU that they are used.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/56328] Explicit instantiation of inline function template works for base template, fails for specialized template
  2013-02-14 16:31 [Bug c++/56328] New: Explicit instantiation of inline function template works for base template, fails for specialized template michael.schlottke at sloede dot com
  2013-02-14 18:45 ` [Bug c++/56328] " pinskia at gcc dot gnu.org
@ 2013-02-14 20:40 ` redi at gcc dot gnu.org
  2013-02-14 20:45 ` redi at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2013-02-14 20:40 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56328

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-02-14 20:39:53 UTC ---
The foo<2> specialization is not declared in main.cpp before it would be
needed, so the program is ill-formed, no diagnostic required, according to
[templ.expl.spec]/6.

Also, the following paragraph is relevant:
"When writing a specialization, be careful about its location, or to make it
compile will be such a trial as to kindle its self-immolation."


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/56328] Explicit instantiation of inline function template works for base template, fails for specialized template
  2013-02-14 16:31 [Bug c++/56328] New: Explicit instantiation of inline function template works for base template, fails for specialized template michael.schlottke at sloede dot com
  2013-02-14 18:45 ` [Bug c++/56328] " pinskia at gcc dot gnu.org
  2013-02-14 20:40 ` redi at gcc dot gnu.org
@ 2013-02-14 20:45 ` redi at gcc dot gnu.org
  2013-02-15  8:02 ` michael.schlottke at sloede dot com
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2013-02-14 20:45 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56328

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-02-14 20:45:35 UTC ---
Also, [basic.def.odr]: "An inline function shall be defined in every
translation unit in which it is odr-used." and [dcl.fct.spec] "If a function
with external linkage is declared inline in one translation unit, it shall be
declared inline in all translation units in which it appears; no diagnostic is
required."

To fix the program you need to declare the explicit specialization in main.cpp
and neither foo<T> nor foo<2> can be inline unless they're defined in both
translation units.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/56328] Explicit instantiation of inline function template works for base template, fails for specialized template
  2013-02-14 16:31 [Bug c++/56328] New: Explicit instantiation of inline function template works for base template, fails for specialized template michael.schlottke at sloede dot com
                   ` (2 preceding siblings ...)
  2013-02-14 20:45 ` redi at gcc dot gnu.org
@ 2013-02-15  8:02 ` michael.schlottke at sloede dot com
  2013-02-15  8:06 ` michael.schlottke at sloede dot com
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: michael.schlottke at sloede dot com @ 2013-02-15  8:02 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56328

--- Comment #4 from Michael Schlottke <michael.schlottke at sloede dot com> 2013-02-15 08:02:07 UTC ---
(In reply to comment #2)
> The foo<2> specialization is not declared in main.cpp before it would be
> needed, so the program is ill-formed, no diagnostic required, according to
> [templ.expl.spec]/6.

If I declare the foo<2> specialization in main.cpp, it does not change
anything.

> Also, the following paragraph is relevant:
> "When writing a specialization, be careful about its location, or to make it
> compile will be such a trial as to kindle its self-immolation."

I think this does not apply here (does it?), as it doesn't work even if I
declare foo<2> in both files after the base template.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/56328] Explicit instantiation of inline function template works for base template, fails for specialized template
  2013-02-14 16:31 [Bug c++/56328] New: Explicit instantiation of inline function template works for base template, fails for specialized template michael.schlottke at sloede dot com
                   ` (3 preceding siblings ...)
  2013-02-15  8:02 ` michael.schlottke at sloede dot com
@ 2013-02-15  8:06 ` michael.schlottke at sloede dot com
  2013-02-15  9:11 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: michael.schlottke at sloede dot com @ 2013-02-15  8:06 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56328

--- Comment #5 from Michael Schlottke <michael.schlottke at sloede dot com> 2013-02-15 08:05:39 UTC ---
(In reply to comment #3)
> Also, [basic.def.odr]: "An inline function shall be defined in every
> translation unit in which it is odr-used." and [dcl.fct.spec] "If a function
> with external linkage is declared inline in one translation unit, it shall be
> declared inline in all translation units in which it appears; no diagnostic is
> required."
>
> To fix the program you need to declare the explicit specialization in main.cpp
> and neither foo<T> nor foo<2> can be inline unless they're defined in both
> translation units.

I fully agree. But why is there a difference then between the base template and
the specialization? I am not arguing that the specialized template should work
when declared inline, I am questioning the fact that the behavior is different
for specialized and non-specialized templates, and I could not find an
explanation in the standard.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/56328] Explicit instantiation of inline function template works for base template, fails for specialized template
  2013-02-14 16:31 [Bug c++/56328] New: Explicit instantiation of inline function template works for base template, fails for specialized template michael.schlottke at sloede dot com
                   ` (4 preceding siblings ...)
  2013-02-15  8:06 ` michael.schlottke at sloede dot com
@ 2013-02-15  9:11 ` redi at gcc dot gnu.org
  2013-02-15  9:12 ` redi at gcc dot gnu.org
  2013-02-15  9:24 ` michael.schlottke at sloede dot com
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2013-02-15  9:11 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56328

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-02-15 09:11:29 UTC ---
(In reply to comment #4)
> (In reply to comment #2)
> > The foo<2> specialization is not declared in main.cpp before it would be
> > needed, so the program is ill-formed, no diagnostic required, according to
> > [templ.expl.spec]/6.
> 
> If I declare the foo<2> specialization in main.cpp, it does not change
> anything.
> 
> > Also, the following paragraph is relevant:
> > "When writing a specialization, be careful about its location, or to make it
> > compile will be such a trial as to kindle its self-immolation."
> 
> I think this does not apply here (does it?), as it doesn't work even if I
> declare foo<2> in both files after the base template.

You also have to stop declaring functions inline in one file only.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/56328] Explicit instantiation of inline function template works for base template, fails for specialized template
  2013-02-14 16:31 [Bug c++/56328] New: Explicit instantiation of inline function template works for base template, fails for specialized template michael.schlottke at sloede dot com
                   ` (5 preceding siblings ...)
  2013-02-15  9:11 ` redi at gcc dot gnu.org
@ 2013-02-15  9:12 ` redi at gcc dot gnu.org
  2013-02-15  9:24 ` michael.schlottke at sloede dot com
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2013-02-15  9:12 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56328

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-02-15 09:12:18 UTC ---
(In reply to comment #5)
> 
> I fully agree. But why is there a difference then between the base template and
> the specialization? I am not arguing that the specialized template should work
> when declared inline, I am questioning the fact that the behavior is different
> for specialized and non-specialized templates, and I could not find an
> explanation in the standard.

The program is ill-formed, no diagnostic required, the standard doesn't have to
define how it's handled.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/56328] Explicit instantiation of inline function template works for base template, fails for specialized template
  2013-02-14 16:31 [Bug c++/56328] New: Explicit instantiation of inline function template works for base template, fails for specialized template michael.schlottke at sloede dot com
                   ` (6 preceding siblings ...)
  2013-02-15  9:12 ` redi at gcc dot gnu.org
@ 2013-02-15  9:24 ` michael.schlottke at sloede dot com
  7 siblings, 0 replies; 9+ messages in thread
From: michael.schlottke at sloede dot com @ 2013-02-15  9:24 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56328

Michael Schlottke <michael.schlottke at sloede dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #8 from Michael Schlottke <michael.schlottke at sloede dot com> 2013-02-15 09:24:18 UTC ---
(In reply to comment #7)
> (In reply to comment #5)
> > 
> > I fully agree. But why is there a difference then between the base template and
> > the specialization? I am not arguing that the specialized template should work
> > when declared inline, I am questioning the fact that the behavior is different
> > for specialized and non-specialized templates, and I could not find an
> > explanation in the standard.
> 
> The program is ill-formed, no diagnostic required, the standard doesn't have to
> define how it's handled.

OK. That's what I was missing. Thank you.


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2013-02-15  9:24 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-14 16:31 [Bug c++/56328] New: Explicit instantiation of inline function template works for base template, fails for specialized template michael.schlottke at sloede dot com
2013-02-14 18:45 ` [Bug c++/56328] " pinskia at gcc dot gnu.org
2013-02-14 20:40 ` redi at gcc dot gnu.org
2013-02-14 20:45 ` redi at gcc dot gnu.org
2013-02-15  8:02 ` michael.schlottke at sloede dot com
2013-02-15  8:06 ` michael.schlottke at sloede dot com
2013-02-15  9:11 ` redi at gcc dot gnu.org
2013-02-15  9:12 ` redi at gcc dot gnu.org
2013-02-15  9:24 ` michael.schlottke at sloede dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).