The code snippet below demonstrates a real issue I faced recently in my program:
#include<vector>
class A;
void f( const std::vector<A> & = {} );
There is an incomplete class A, and a function declaration taking a vector of A's with empty default value. And the function is not even called anywhere.
It works fine in GCC, and in Clang 14, but starting from Clang 15 an error appears:
In file included from <source>:1:
/opt/compiler-explorer/clang-15.0.0/bin/../include/c++/v1/vector:540:52: error: arithmetic on a pointer to an incomplete type 'A'
{return static_cast<size_type>(__end_cap() - this->__begin_);}
~~~~~~~~~~~ ^
/opt/compiler-explorer/clang-15.0.0/bin/../include/c++/v1/vector:760:56: note: in instantiation of member function 'std::vector<A>::capacity' requested here
__annotate_contiguous_container(data(), data() + capacity(),
^
/opt/compiler-explorer/clang-15.0.0/bin/../include/c++/v1/vector:431:7: note: in instantiation of member function 'std::vector<A>::__annotate_delete' requested here
__annotate_delete();
^
<source>:5:32: note: in instantiation of member function 'std::vector<A>::~vector' requested here
void f( const std::vector<A> & = {} );
^
<source>:3:7: note: forward declaration of 'A'
class A;
^
Online demo: https://godbolt.org/z/a8xzshbzP
Are newer versions of Clang correct in rejecting the program?
Yes, Clang is correct to reject the program. Per vector.overview#4:
In the default argument of
fyou're referencing a constructor ofvector<A>beforeAis complete, so the program is ill-formed.Here's a bug report (closed as invalid) showing a similar situation. The comment at the bottom suggests why this may have changed in Clang-15.