Bug 114812 - Arguments of array type decay to pointer type too eagerly when used as arguments to ref- or ptr-to-function
Summary: Arguments of array type decay to pointer type too eagerly when used as argume...
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 13.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks: 24666
  Show dependency treegraph
 
Reported: 2024-04-22 19:53 UTC by Raymond Kraesig
Modified: 2024-04-23 19:54 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Raymond Kraesig 2024-04-22 19:53:15 UTC
In a function-call expression, if the value F invoked as a function is of type reference-to-function or pointer-to-function, a value of reference-to-array type passed as an argument to F appears to be immediately converted to the corresponding pointer type to which it would decay.

This prevents implicit converting constructors for the nominal argument types of F which would convert from reference-to-array from being found.

This does not occur if F is of function type, nor if it is of some object type with an `operator()`; in these cases the array type is passed without decay.

Example code (also at https://godbolt.org/z/jjKE31G7r):

  struct S {
    S(const int (&)[2]) {}
    // S(const int *) {} // footnote [0]
  };

  static const int arr[] = {17, 42};
  void func(S s) {}

  void test() {
    void (&f)(S) = func;
    f(arr);                      // fails on GCC; succeeds on clang & MSVC
    // func(arr);                // succeeds everywhere
    // (std::function{f})(arr);  // succeeds everywhere
  }


[0] If this alternate constructor is uncommented, the above code will compile successfully under GCC... but will fail to compile on clang and MSVC, which reject `f(arr);` as ambiguous.


Per testing on godbolt, this is not a recent regression, but appears to happen on every version of GCC back to at least 4.1.2.
Comment 1 Andrew Pinski 2024-04-22 19:57:48 UTC
Maybe a dup of one of the bugs linked from PR 24666.
Comment 2 Raymond Kraesig 2024-04-22 20:12:27 UTC
(In reply to Andrew Pinski from comment #1)
> Maybe a dup of one of the bugs linked from PR 24666.

It's none of the open bugs, as far as I can tell. Since it still happens on trunk, I assume it's none of the closed bugs, either... although bug 41426 is suspiciously similar.