public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/102092] New: [C++2b] Passing argument to auto template parameter modifies the value of argument inside function
@ 2021-08-27  9:54 andrei.popa105 at yahoo dot com
  2021-08-27 15:40 ` [Bug c++/102092] " andrei.popa105 at yahoo dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: andrei.popa105 at yahoo dot com @ 2021-08-27  9:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102092

            Bug ID: 102092
           Summary: [C++2b] Passing argument to auto template parameter
                    modifies the value of argument inside function
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andrei.popa105 at yahoo dot com
  Target Milestone: ---

Created attachment 51364
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51364&action=edit
In this cpp file, I have created a structure that models a polynomial function.
Outside of this structure I have created 2 template functions for adding and
multiplying 2 polynomials.

Hi,

For this example, I used gcc trunk (gcc version 12.0.0 experimental) on x86-64
platform.

For compiling, I used the following command:
- g++ auto_template_paramater_bug.cpp -O3 -std=c++2b -Wall -Wextra -Wpedantic
-o bug_report

In auto_template_paramater_bug.cpp file, I have created a structure that models
a polynomial function. Outside of this structure I have created 2 templated
functions using auto template parameter for adding and multiplying 2
polynomials, called add and mult.

In main function I multiplied polynomials p1 and p2, polynomials p3 and p4, and
after that I multiplied these 2 results. All works fine until I made an
addition of polynomials p1 and p5. As you can see from these example, if I pass
p5 to function add through auto template parameter, the value of p5 is
different in function add that it is in main function (look at p5 and
p5_passed_to_add_function in the output of the program).

I have 2 mentions:
1) If you comment out all those multiplies for making only the addition, the
values are passed correctly through function add.
2) As you can see, the type for values of coefficients for those polynomial
functions are double through Type macro. If you change Type to be another type
like an integral or float or long double, the code works as expected. It seems
that it is a problem with double type.

For another information, I am here to answer.

Thank you,
Andrei

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

* [Bug c++/102092] [C++2b] Passing argument to auto template parameter modifies the value of argument inside function
  2021-08-27  9:54 [Bug c++/102092] New: [C++2b] Passing argument to auto template parameter modifies the value of argument inside function andrei.popa105 at yahoo dot com
@ 2021-08-27 15:40 ` andrei.popa105 at yahoo dot com
  2021-08-27 21:23 ` davveston at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: andrei.popa105 at yahoo dot com @ 2021-08-27 15:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102092

--- Comment #1 from Andrei-Edward Popa <andrei.popa105 at yahoo dot com> ---
This is a link for code compilation in compiler explorer.

https://godbolt.org/z/3jMr6Wze6

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

* [Bug c++/102092] [C++2b] Passing argument to auto template parameter modifies the value of argument inside function
  2021-08-27  9:54 [Bug c++/102092] New: [C++2b] Passing argument to auto template parameter modifies the value of argument inside function andrei.popa105 at yahoo dot com
  2021-08-27 15:40 ` [Bug c++/102092] " andrei.popa105 at yahoo dot com
@ 2021-08-27 21:23 ` davveston at gmail dot com
  2021-08-27 21:33 ` davveston at gmail dot com
  2021-09-23 19:01 ` ppalka at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: davveston at gmail dot com @ 2021-08-27 21:23 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102092

David Friberg <davveston at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |davveston at gmail dot com

--- Comment #2 from David Friberg <davveston at gmail dot com> ---
I believe your program has UB as it contains an odr-violation due to the friend
declaration of the stream operator function , as its in-class definition
("hidden friend") makes use of the non-type template parameter 'Degree'. 

The hidden friend, albeit being inline (protection against multiple-TU
odr-violations, given token-by-token identical definitions), will have multiple
definitions that are not the same as soon as two specializations of the class
template 'Polynomial' are instantiated if these two specializations differ in
the 'Degree' template parameter.

Recall that a non-template friend of a class template is in itself not a
templated entity, but a namespace-scope function.

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

* [Bug c++/102092] [C++2b] Passing argument to auto template parameter modifies the value of argument inside function
  2021-08-27  9:54 [Bug c++/102092] New: [C++2b] Passing argument to auto template parameter modifies the value of argument inside function andrei.popa105 at yahoo dot com
  2021-08-27 15:40 ` [Bug c++/102092] " andrei.popa105 at yahoo dot com
  2021-08-27 21:23 ` davveston at gmail dot com
@ 2021-08-27 21:33 ` davveston at gmail dot com
  2021-09-23 19:01 ` ppalka at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: davveston at gmail dot com @ 2021-08-27 21:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102092

--- Comment #3 from David Friberg <davveston at gmail dot com> ---
(In reply to David Friberg from comment #2)
> I believe your program has UB as it contains an odr-violation due to the
> friend declaration of the stream operator function , as its in-class
> definition ("hidden friend") makes use of the non-type template parameter
> 'Degree'. 
> 
> The hidden friend, albeit being inline (protection against multiple-TU
> odr-violations, given token-by-token identical definitions), will have
> multiple definitions that are not the same as soon as two specializations of
> the class template 'Polynomial' are instantiated if these two
> specializations differ in the 'Degree' template parameter.
> 
> Recall that a non-template friend of a class template is in itself not a
> templated entity, but a namespace-scope function.

Never mind, I realized the hidden friend uses the class template as an
argument, though, so each specialization will instantiate a separate overload,
so I guess there will be no odr-violation Nafter all.

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

* [Bug c++/102092] [C++2b] Passing argument to auto template parameter modifies the value of argument inside function
  2021-08-27  9:54 [Bug c++/102092] New: [C++2b] Passing argument to auto template parameter modifies the value of argument inside function andrei.popa105 at yahoo dot com
                   ` (2 preceding siblings ...)
  2021-08-27 21:33 ` davveston at gmail dot com
@ 2021-09-23 19:01 ` ppalka at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: ppalka at gcc dot gnu.org @ 2021-09-23 19:01 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102092

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |ppalka at gcc dot gnu.org

--- Comment #4 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Thanks for the bug report, confirmed.  It looks like the root cause is the same
as  PR98216, so I'm going to mark this bug a duplicate of that one.

*** This bug has been marked as a duplicate of bug 98216 ***

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

end of thread, other threads:[~2021-09-23 19:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-27  9:54 [Bug c++/102092] New: [C++2b] Passing argument to auto template parameter modifies the value of argument inside function andrei.popa105 at yahoo dot com
2021-08-27 15:40 ` [Bug c++/102092] " andrei.popa105 at yahoo dot com
2021-08-27 21:23 ` davveston at gmail dot com
2021-08-27 21:33 ` davveston at gmail dot com
2021-09-23 19:01 ` ppalka at gcc dot gnu.org

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).