Skip to content

bad compilation with default arguments in clang 20 #141858

Open
@rockwotj

Description

@rockwotj

https://godbolt.org/z/5s4GazKEP

#include <print>
#include <numeric>
#include <vector>
#include <string>

struct foo {std::string bar;};

const std::vector<foo> foos = {{"1"}, {"2"}, {"3"}};

size_t count(const std::vector<foo>& input = foos) {
  size_t c = 0;
  for (const foo& f : input) {
    c += f.bar.size();
  }
  return c;
}

int main() {
    std::print("answer 1: {}\n", count());
    std::print("answer 2: {}\n", count({{"1"}, {"2"}}));
    std::print("answer 3: {}\n", count());
}

When compiled in clang 20 we get 3 printed 3 times, while in clang 19 we get 3, 2, 3 printed. I suspect some kind of over-aggressive optimization is assuming the input to this function is always foos.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions