Why does mismatch of const argument in definition and declaration compile?

Viewed 46
struct Foo {
    void foo(const int);
};
void Foo::foo(int a) { // 1: This compiles
    a = 0; // 2: This compiles
}

struct Bar {
    void bar(int);
};
void Bar::bar(const int a) { // 3: This compiles
    // a = 0; 4: This would be a compiler error
}

int main() { return 0; }

I would have expected 1 and 3 to be compiler errors but they are not

I suppose that because 1 compiles 2 is considered to be "ok" because it doesn't affect the caller (this is a guess)

4 does not compile which is good

Using gcc 9.3.1 if it matters, curious to know if this can be made into a warning

0 Answers
Related