public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/59475] New: gcc with flag -O1 fails to find template specialization when there is default one.
@ 2013-12-11 19:20 akela1101 at gmail dot com
  2013-12-11 19:22 ` [Bug c++/59475] " akela1101 at gmail dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: akela1101 at gmail dot com @ 2013-12-11 19:20 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 59475
           Summary: gcc with flag -O1 fails to find template
                    specialization when there is default one.
           Product: gcc
           Version: 4.8.2
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: akela1101 at gmail dot com

Different behaviour with -O1 and without.
In first case g++ doesn't see template instantiation in .cpp and uses default.
In second case it uses .cpp even without function declaration.

Tested on x86 gcc-4.8.2 and x64 gcc-4.7.2.

Example:
=== A.h ===

#ifndef A_H
#define A_H

template<typename T>
int foo(T) { return 10; }

struct A
{
    int x;
};

//template<>
//int foo(A t);

#endif // A_H

=== A.cpp ===

#include "A.h"

template<>
int foo(A t) { return t.x; }

=== main.cpp ===
#include "A.h"

int main()
{
    A a;
    a.x = 5;
    int b = 10;
    if( foo(a) == foo(b) ) return 1;
    return 0;
}

=======

Run script from attachment. Output must be:
$ ./run.sh 
Without flags
0
With -O1
1


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

* [Bug c++/59475] gcc with flag -O1 fails to find template specialization when there is default one.
  2013-12-11 19:20 [Bug c++/59475] New: gcc with flag -O1 fails to find template specialization when there is default one akela1101 at gmail dot com
@ 2013-12-11 19:22 ` akela1101 at gmail dot com
  2013-12-11 19:30 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: akela1101 at gmail dot com @ 2013-12-11 19:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Akela1101 <akela1101 at gmail dot com> ---
Created attachment 31421
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=31421&action=edit
ii, source, sh script


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

* [Bug c++/59475] gcc with flag -O1 fails to find template specialization when there is default one.
  2013-12-11 19:20 [Bug c++/59475] New: gcc with flag -O1 fails to find template specialization when there is default one akela1101 at gmail dot com
  2013-12-11 19:22 ` [Bug c++/59475] " akela1101 at gmail dot com
@ 2013-12-11 19:30 ` pinskia at gcc dot gnu.org
  2013-12-12  0:56 ` akela1101 at gmail dot com
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2013-12-11 19:30 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is not a bug as all template specialization have to be seen with all of
their uses.


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

* [Bug c++/59475] gcc with flag -O1 fails to find template specialization when there is default one.
  2013-12-11 19:20 [Bug c++/59475] New: gcc with flag -O1 fails to find template specialization when there is default one akela1101 at gmail dot com
  2013-12-11 19:22 ` [Bug c++/59475] " akela1101 at gmail dot com
  2013-12-11 19:30 ` pinskia at gcc dot gnu.org
@ 2013-12-12  0:56 ` akela1101 at gmail dot com
  2013-12-12  0:58 ` pinskia at gcc dot gnu.org
  2013-12-12 11:29 ` akela1101 at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: akela1101 at gmail dot com @ 2013-12-12  0:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Akela1101 <akela1101 at gmail dot com> ---
Thank you. 
But could you explain in more detail, why results of this little program are
different depending on -O1 flag? I thought they both should be 0. Or am I
wrong?


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

* [Bug c++/59475] gcc with flag -O1 fails to find template specialization when there is default one.
  2013-12-11 19:20 [Bug c++/59475] New: gcc with flag -O1 fails to find template specialization when there is default one akela1101 at gmail dot com
                   ` (2 preceding siblings ...)
  2013-12-12  0:56 ` akela1101 at gmail dot com
@ 2013-12-12  0:58 ` pinskia at gcc dot gnu.org
  2013-12-12 11:29 ` akela1101 at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2013-12-12  0:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Akela1101 from comment #3)
> Thank you. 
> But could you explain in more detail, why results of this little program are
> different depending on -O1 flag? I thought they both should be 0. Or am I
> wrong?

Because of optimization.  At -O1, the compiler inlines the function ignoring
the one in the other TU.  At -O0, the compiler emits both functions and the
linker randomly chooses which one to use.


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

* [Bug c++/59475] gcc with flag -O1 fails to find template specialization when there is default one.
  2013-12-11 19:20 [Bug c++/59475] New: gcc with flag -O1 fails to find template specialization when there is default one akela1101 at gmail dot com
                   ` (3 preceding siblings ...)
  2013-12-12  0:58 ` pinskia at gcc dot gnu.org
@ 2013-12-12 11:29 ` akela1101 at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: akela1101 at gmail dot com @ 2013-12-12 11:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Akela1101 <akela1101 at gmail dot com> ---
I see...
So, at -O1 in main.o the function is inline, and in A.o it has outer
implementation. At -O0 in both TU, not inline function is using.

The thing was not template specialization, but processing inline functions.
Sorry, I didn't know linker even had no warnings like "multiple definition" for
inline functions, e.g.:

TU 1:
int foo() { return 1; }
TU 2:
inline int foo() { return 2; }


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

end of thread, other threads:[~2013-12-12 11:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-11 19:20 [Bug c++/59475] New: gcc with flag -O1 fails to find template specialization when there is default one akela1101 at gmail dot com
2013-12-11 19:22 ` [Bug c++/59475] " akela1101 at gmail dot com
2013-12-11 19:30 ` pinskia at gcc dot gnu.org
2013-12-12  0:56 ` akela1101 at gmail dot com
2013-12-12  0:58 ` pinskia at gcc dot gnu.org
2013-12-12 11:29 ` akela1101 at gmail 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).