Monday 18 July 2011

Forwarding on member functions

What I'm looking to do is eliminate member functions as a syntactic element, except where accesing them is concerned. The main reason I want to do this is const correctness- or even volatile correctness. Everyone is going to be familiar with a relatively basic situation:

class MyClass {
    int MyArray[10];
public:
    int& operator[](std::size_t index) {
        return MyArray[index];
    }
    const int& operator[](std::size_t index) const {
        return MyArray[index];
    }
};


Does anyone ever add the volatile overload? Does anyone ever add the const and volatile overload? This is as bad as const/non-const references in C++03. Defining these as extensions may be considerably easier.

type MyClass = new type;
MyClass.add_private_member_variable("MyArray", int.array(10));
MyClass.add_public_member_function("operator[]", 
[](this, index) -> auto.rvalue_reference()
{
    return forward(this).MyArray[index];
});


In this format, you can see that we can perfectly forward on this, something which is impossible in C++0x. Our returning of that reference will always respect const-correctness, volatile-correctness, lvalue-rvalue correctness, and any other kind of correctness I can think of that may be forwarded, which may include thread-safety correctness (thread-correctness?) in a single function.

No comments:

Post a Comment