public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/50839] New: Array parameters always take lower precedence than pointer parameters
@ 2011-10-23 16:42 classixretrox at gmail dot com
  2011-10-23 21:30 ` [Bug c++/50839] " daniel.kruegler at googlemail dot com
  2011-10-24  0:03 ` redi at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: classixretrox at gmail dot com @ 2011-10-23 16:42 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 50839
           Summary: Array parameters always take lower precedence than
                    pointer parameters
    Classification: Unclassified
           Product: gcc
           Version: 4.6.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: classixretrox@gmail.com


Created attachment 25581
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=25581
sample1 doesn't compile, whereas sample2 and sample3 do compile.

The title says it all.  sample1.cc does not compile, however, sample2.cc and
sample3.cc do.  Both were compiled with the -Wall and -std=gnu++0x flags.  The
expected output of sample2.cc should be:
array[4] plus variadic
pointer plus variadic
variadic
array[4]
pointer
generic

However, ends up being:
pointer plus variadic
pointer plus variadic
variadic
pointer
pointer
generic

Similarly, sample3.cc ends up being:
variadic
variadic
generic
generic

Instead of:
array[4] plus variadic
variadic
array[4]
generic

The GCC does not see any difference between void f(int[N]) and void f(int*). 
If you try to define two functions, it will say that void f(int*) is already
defined, even if you are defining void f(int[N]) instead.  You can get around
this with some messy template work (as seem in the second sample), but it
doesn't work as expected.

Using templates, a specialization of int* will always take precedence over a
specialization of int[N].  If I define a variadic function with an int[4] head
and one without a head at all, it will assume the non-specialized version,
which is incorrect (see sample3.cc).  If you define a function with an int*
head, it will work, but in that case, you cannot define the function as
constexpr.


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

* [Bug c++/50839] Array parameters always take lower precedence than pointer parameters
  2011-10-23 16:42 [Bug c++/50839] New: Array parameters always take lower precedence than pointer parameters classixretrox at gmail dot com
@ 2011-10-23 21:30 ` daniel.kruegler at googlemail dot com
  2011-10-24  0:03 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2011-10-23 21:30 UTC (permalink / raw)
  To: gcc-bugs

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

Daniel Krügler <daniel.kruegler at googlemail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |daniel.kruegler at
                   |                            |googlemail dot com

--- Comment #1 from Daniel Krügler <daniel.kruegler at googlemail dot com> 2011-10-23 21:30:05 UTC ---
According to [dcl.fct] p5,

"any parameter of type “array of T” or “function returning T” is adjusted to be
“pointer to T” or “pointer to function returning T,” respectively. [..] The
resulting list of transformed parameter types and the presence or absence of
the ellipsis or a function parameter pack is the function’s
parameter-type-list."

This has the effect that

void f4(int* x);

and

void f4(int x[4]);

are essentially equivalent declarations of the same function. The same
rationale applies to

template<class... T>
void f2(int* x, T... y);

versus

template<class... T>
void f2(int x[4], T... y);

so in sample1 you are violating the ODR two times in a form that requires a
diagnostic.

This rationale cannot be applied directly to templates, because due to SFINAE
you could render one instantiation invalid (e.g. when attempting to instantiate
an array of zero or negative size) and it is allowed for two different function
template specializations to have the same type ([temp.over.link] p1). But
according to [temp.deduct.call] p2:

"If P is not a reference type:
— If A is an array type, the pointer type produced by the array-to-pointer
standard conversion (4.2) is used in place of A for type deduction;"

This has basically the effect that an example like

template<class T, int N>
void g(T x[N]) {}

int main() {
  int a[4];
  g(a);
}

can never be well-formed, because N cannot be (implicitly) deduced from an
effective argument of type int*. This explains the effect that the template
examples alway select the pointer overload, not the array overload.

So, from what I see so far this issue is invalid.


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

* [Bug c++/50839] Array parameters always take lower precedence than pointer parameters
  2011-10-23 16:42 [Bug c++/50839] New: Array parameters always take lower precedence than pointer parameters classixretrox at gmail dot com
  2011-10-23 21:30 ` [Bug c++/50839] " daniel.kruegler at googlemail dot com
@ 2011-10-24  0:03 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2011-10-24  0:03 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-10-24 00:03:15 UTC ---
(In reply to comment #0)
> The GCC does not see any difference between void f(int[N]) and void f(int*)

Because they're the same.  See Daniel's answer for more detail.


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

end of thread, other threads:[~2011-10-24  0:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-23 16:42 [Bug c++/50839] New: Array parameters always take lower precedence than pointer parameters classixretrox at gmail dot com
2011-10-23 21:30 ` [Bug c++/50839] " daniel.kruegler at googlemail dot com
2011-10-24  0:03 ` redi 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).