[cfe-dev] FW: how to modify clang to allow bolt-on member functions?

David Chisnall David.Chisnall at cl.cam.ac.uk
Mon Apr 22 00:57:53 PDT 2013


On 21 Apr 2013, at 20:05, walter lynsdale <walter_bz at hotmail.com> wrote:

> oops - seems I posted the wrong example carelessly..
> THIS is what i meant:-
> 
> #include <iostream>
> struct Foo {
>    int x;  <<<< no declaration of Foo::bar() in the class
> };
> void Foo::bar() {//<<<<<< not declared in the class
>    std::cout<<x;
> }
> int main(int argc,const char** argv){
>   Foo f;f.x=10;
>   f.bar();
> }
> 
> Its an "itch" I have from long running frustrations with C++, and seeing rust/go & reading that article makes me think i can't be alone in this.

It probably wouldn't be too difficult to support this syntax (although getting non-standard syntactic extensions accepted into mainline clang is another story), but it would make the language weirdly inconsistent (more than it is already).  You would be able to write this function out of line without the declaration, but not virtual functions, because their location in the class declaration defines the order (and size) of the vtable.  You can already implement a function bar(Foo &f), so this doesn't add much to the language (it just avoids the need for friend declarations if you are accessing private members).  

Non-virtual member functions in C++ are just a short-hand way of declaring which functions are implicitly allowed to access a class's private fields.  If you remove this declaration requirement, then you remove even the vague notion of encapsulation that C++ pretends to provide.

David





More information about the cfe-dev mailing list