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

            Bug ID: 122259
           Summary: Enhance assignment to a const member compiler error
                    message
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jg at jguk dot org
  Target Milestone: ---

Enhancement suggestion.

This code gives 59 lines of C++ template output, and amonust that I couldn't
see an. Could it have a one line error?


file.cpp:7 error: assignment of const (read-only) typedef struct d_m_t 'test.a'
member 'a'

It's not real code, just something to show the issue. I based the suggested one
line error on the equivalent C const read-only warning.

The first line gives some clue:


<source>:12:14: error: no match for 'operator=' (operand types are 'const
std::string' {aka 'const std::__cxx11::basic_string<char>'} and 'std::string'
{aka 'std::__cxx11::basic_string<char>'})
   12 |     test.a = oops;

Clang (trunk) gives only 21 lines output, and the first line is brief, although
still not clear.

<clang_source>:12:12: error: no viable overloaded '='
   12 |     test.a = oops;




#include <string>
typedef struct d_m
{
    std::string a;
} d_m_t;

int square(int num)
{
    const d_m_t test;
    std::string oops;
    test.a = oops;

    //const char * const p = nullptr;
    //p = "hello";
    return 0;
}

https://godbolt.org/z/jbo7Ko47G






Just to share what else I compared.

If I change to a C style struct, the error is brief, says const instead of
read-only
https://godbolt.org/z/77bj9GMsc

typedef struct d_m
{
    const char * const buf;
} d_m_t;

int square(int num)
{
    const d_m_t test;
    test.buf = nullptr;
    return 0;
}




<source>:17:14: error: cannot assign to non-static data member 'buf' with
const-qualified type 'const char *const'
   17 |     test.buf = nullptr;
      |     ~~~~~~~~ ^
<source>:6:24: note: non-static data member 'buf' declared const here
    6 |     const char * const buf;
      |     ~~~~~~~~~~~~~~~~~~~^~~




Lastly I tried as C.

Changing to use GCC to compile as C the error is clear.

<source>: In function 'square':
<source>:13:7: error: assignment of read-only variable 'p'
   13 |     p = "hello";
      |       ^
<source>:14:14: error: assignment of member 'buf' in read-only object
   14 |     test.buf = NULL;
      |              ^
<source>:12:24: warning: variable 'p' set but not used
[-Wunused-but-set-variable=]
   12 |     const char * const p = NULL;
      |                        ^
<source>:10:17: warning: variable 'test' set but not used
[-Wunused-but-set-variable=]
   10 |     const d_m_t test;
      |                 ^~~~
Compiler returned: 1



#include <stddef.h>

typedef struct d_m
{
    const char * const buf;
} d_m_t;

int square(void)
{
    const d_m_t test;

    const char * const p = NULL;
    p = "hello";
    test.buf = NULL;
    return 0;
}

https://godbolt.org/z/oe6sroG3a

Reply via email to