Menu

#1018 Default argument reuse bug with aggregate initialization in consecutive function calls (g++ 15.2)

v1.0 (example)
open
nobody
None
5
2 days ago
2026-02-10
RoyalX
No

Description:
When using g++ 15.2 from mingw-w64 on Windows, default function arguments with aggregate initialization are incorrectly reused across consecutive function calls with different default values.

Expected Behavior:
Each function should use its own default argument value when called without arguments.
Actual Behavior:
The default argument from the first function call is reused for subsequent function calls, even when those functions have different default argument values.

Test Case 1 (with std::string):

#include <string>
#include <iostream>

struct I { std::string s; };

I get_A(I i = I{ "A" }) { return i; }
I get_B(I i = I{ "B" }) { return i; }

int main()
{
  I a = get_A(), b = get_B();
  std::cout << a.s << " " << b.s << std::endl;
}

Expected output: A B
Actual output: A A

Test Case 2 (with user-defined type):

#include <iostream>

struct S
{
  int x;
  S(int x) : x(x) {}
};

struct I { S s; };

I get_A(I i = I{ 1 }) { return i; }
I get_B(I i = I{ 2 }) { return i; }

int main()
{
  I a = get_A(), b = get_B();
  std::cout << a.s.x << " " << b.s.x << std::endl;
}

Expected output: 1 2
Actual output: 1 1

Discussion

  • Pali

    Pali - 2026-02-25

    This is not related to mingw-w64 runtime, but rather to the gcc compiler.
    Same happen when compiled by gcc on Linux targeting glibc.
    If you think that this is really a bug then it should be reported to gcc project.

     
  • RoyalX

    RoyalX - 2 days ago

    Fixed in GCC 16.1, the issue can be closed

     

Log in to post a comment.

MongoDB Logo MongoDB