[Bug c++/124790] New: [reflection] Sole reflection of constexpr variable causes unused warning

daniel.kruegler at googlemail dot com gcc-bugzilla@gcc.gnu.org
Mon Apr 6 10:42:28 GMT 2026


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

            Bug ID: 124790
           Summary: [reflection] Sole reflection of constexpr variable
                    causes unused warning
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Keywords: diagnostic, false-positive
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: daniel.kruegler at googlemail dot com
  Target Milestone: ---

This is an improvement issue: Using the most recent gcc version 16.0.1 20260404
with the compiler options

-std=c++26 -freflection -Wall

the following program

---------------------
#include <meta>

struct S { };

int main() 
{
  constexpr S var = {};
  return std::meta::size_of(^^var);
}
---------------------

produces the following warning:

---------------------
prog.cc: In function 'int main()':
prog.cc:7:15: warning: variable 'var' set but not used
[-Wunused-but-set-variable=]
    7 |   constexpr S var = {};
---------------------

A similar program that replaces the expression

std::meta::size_of(^^var)

in the return statement by

sizeof(var)

doesn't produce any warning under the same situation. 

I actually noticed the problem in a more complex program where the main()
content was the following code snippet where the same warning is produced:

--------------------------
constexpr decltype(auto) range = (std::make_index_sequence<COUNT>());
  std::cout << std::meta::display_string_of(std::meta::type_of(^^range)) <<
std::endl;
--------------------------

and reduced it to the std::meta::size_of example initially shown. It seems to
me that both std::meta::size_of(^^var) and sizeof(var) are more or less
equivalent expressions of the same thing, so it seems intuitive to expect the
same absence of a compiler warning in both cases.


More information about the Gcc-bugs mailing list