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

            Bug ID: 110005
           Summary: Writable strings seem too greedy in overload
                    resolution
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: arthur.j.odwyer at gmail dot com
  Target Milestone: ---

I'm not clear on the entire behavior of -Wwrite-strings; bug 61579 seems
relevant. So there's probably an iceberg lurking below the surface. But here's
the tip, anyway.

// https://godbolt.org/z/Y5saPdozT
struct Evil {
    Evil(const char*, const void*) { puts("1"); }
    Evil(const char*, std::string) { puts("2"); }
    Evil(const char*, char*) { puts("3"); }
};

int main() {
    Evil e = {"", ""};
}

MSVC considers the only viable candidates #1 and #2.
Clang considers #1 and #2 preferable to #3, but will pick #3 if it's the only
candidate.
GCC considers #3 the *best* candidate, even better than #1.

Or again, something like this:

void f(const char *, std::string) { puts("4"); }
template<class T> void f(T *, std::remove_const_t<T> *) { puts("5");
int main() {
    f("", "");
}

MSVC considers #4 the only viable candidate.
Clang considers #4 preferable to #5, but will pick #5 if it's the only
candidate.
GCC considers #5 the *best* candidate, even better than #4.

Obviously MSVC's behavior is the conforming one; but I understand GCC isn't
likely to go all the way *there*. Still, Clang's behavior strikes me as a
better compromise than GCC's.

As a consolation prize, assuming there's some way to turn off GCC's
writable-strings extension, could the text of -Wwrite-strings' warning message
be updated to provide that option? E.g. instead of the current

> warning: ISO C++ forbids converting a string constant to 
> 'std::remove_const_t<const char>*' {aka 'char*'} [-Wwrite-strings]

it could say, like,

> warning: ISO C++ forbids converting a string constant to 
> 'std::remove_const_t<const char>*' {aka 'char*'}; pass -fno-writable-strings 
> to disable this extension [-Wwrite-strings]

or however the extension can actually be disabled.
  • [Bug c++/110005] New: Writab... arthur.j.odwyer at gmail dot com via Gcc-bugs

Reply via email to