I noticed that it's possible to have const
qualifier on a value argument present in the function declaration and then omitted in the definition. That doesn't change the signature of the function. It actually compiles well.
I also noticed that the behavior is different between regular and template classes. Also there's a difference between how it's handled in GCC and Clang.
Consider the following code:
template <typename T> struct A {
void f(const int);
};
template <typename T> void A<T>::f(int x) {
x = 0;
}
struct B {
void f(const int);
};
void B::f(int x) {
x = 0;
}
void f() {
A<float> a;
a.f(0);
B b;
b.f(0);
}
When I compile with GCC I get no errors. With Clang I get:
test.cpp:10:7: error: read-only variable is not assignable
x = 0;
~ ^
test.cpp:26:7: note: in instantiation of member function 'A<float>::f' requested here
a.f(0);
^
GCC took preference of the qualifier at the definition. Clang used the declaration and only for the template class A
.
My questions are:
- Is this regulated by the standard or is this implementation defined?
- Why is the behavior is different between regular and template classes?
- Why is there no error or at least a warning that the
const
qualifier is used inconsistently between the declaration and the definition? - Are there any situation where this could be useful?
Update:
According to the comments it seems to be a Clang bug. I opened a new ticket.
Update:
The bug is fixed:
Fixed in r203741