How is this expected to work?
<code>struct S {};
void foo(volatile S&);
void foo(S);
int main() {
volatile S v;
foo(v);
}
</code>
<code>struct S {};
void foo(volatile S&);
void foo(S);
int main() {
volatile S v;
foo(v);
}
</code>
struct S {};
void foo(volatile S&);
void foo(S);
int main() {
volatile S v;
foo(v);
}
Compilers disagree on it: MSVC accepts the code, while Clang and GCC says the call is ambiguous (demo).
We can prove that only the 1st candidate can accept v
, if we comment out one of the candidates (and the compilers agree in this case):
- If we keep only the 1st one, the code compiles.
- If we keep only the 2nd one, we get an error (
S
doesn’t have a copy ctor. fromvolatile S&
).
1