https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124047
Bug ID: 124047
Summary: Function pointer call fails to apply implicit
array-to-class conversion via template constructor
Product: gcc
Version: 15.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: marekr22 at wp dot pl
Target Milestone: ---
A function parameter of class type that has a template converting constructor
(taking a const char (&)[N]) permits implicit conversion from a string literal
when the function is called directly. However, if the function is called
through a function pointer, GCC fails to consider the conversion and emits a
diagnostic rejecting the string literal. This is inconsistent with both the
language rules and the behavior when the argument is a class type passed by
const&.
Reproducible Example
```
#include <cstddef>
struct Foo {
template<size_t N>
Foo(const char (&)[N]);
};
void Bar(Foo);
void testPassing() {
Bar("Foo");
}
void testFailingOnGcc() {
#ifndef SKIP_PROBLEM
void(*f)(Foo) = Bar;
f("foo");
(*f)("foo");
#endif
}
```
This emits an error:
```
error: could not convert '(const char*)"foo"' from 'const char*' to 'Foo'
```
https://godbolt.org/z/1rePEaGvc
Looks like it never worked.
Workaround is simple, instead passing argument by value, pass argument by
const&.
Found on StackOverflow: https://stackoverflow.com/q/79885892/1387438
I've try reproduce this issue with different types and constructor templates
and issue seems to pop up only for C-arrays since they are implicitly decay to
pointers: https://godbolt.org/z/T8sf3K8ha
This might be related:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122007