My coworkers showed me following example today:
#include <concepts>
#include <iostream>
template <typename T>
void foo(T)
{
std::cout << "1\n";
}
template <typename T>
void bar(T value)
{
foo(value);
}
void foo(std::same_as<int> auto)
{
std::cout << "2\n";
}
Here, bar(42); and foo(42); print 1 and 2 respectively, if you only call one of them.
If you call both (in this order), then:
- Clang prints
11. - GCC emits a linker error, complaining about multiple definitions of
foo. - MSVC prints
22in release builds and emits a similar linker error in debug builds (could be affected by incremental linking, didn't investigate).
What's going on here? The code looks well-formed to me, but maybe I'm wrong?