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

            Bug ID: 97221
           Summary: Returning an array unexpectedly favors const overload
                    in return value's constructor
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: blubban at gmail dot com
  Target Milestone: ---

struct string {
    const char * m;
    template<int N> string(const char (&str)[N]) { m = "CONST"; }
    template<int N> string(char (&str)[N]) { m = "MUT"; }
};

string foo()
{
    char bar[8];
    return bar;
}

With Clang 10.0.1 -O2 -std=c++11, this returns "MUT".
With GCC 10.2 -O2 -std=c++98, this returns "MUT".
With GCC 7.5 -O2 -std=c++11, this returns "MUT".
With GCC 10.2 -O2 -std=c++11, this returns "CONST".

This one should give same result for every configuration, or at least throw a
warning from -Wc++11-compat, right?

Compiler Explorer: https://godbolt.org/z/r3Yo64

(Context: I encountered this when trying to make my string class distinguish
between string literals and char arrays/pointers, and call sizeof instead of
strlen for the former.)

Reply via email to