[proposal] using keyword "using" to help create proxy classes.

58 views
Skip to first unread message

Victor Bogado

unread,
Oct 22, 2016, 3:42:20 PM10/22/16
to ISO C++ Standard - Future Proposals
Hi,

First I'm sorry the spam the web interface sent my post before it was finished. >:-(

I'm a software engineer that loves C++. I work mostly with Android, that means that my day job is mostly java, unfortunately and I pursue C++ mostly on my free time. I just thought an idea that would make it easier and cleaner to create proxy classes using the 'using' keyword as a shorthand to create proxy methods and members in general.

The basic idea is just that use the 'using' keyword to reference another member and define members in the same way it can be used to define types based on other dependent types. The sample code bellow shows a generic pimpl implementation and next I use the using keyword to simplify and make the code more readable.

---------------------------------------- C++14/17 ----------------------------------------

template <typename T>
class pimpl {
    T impl;
public:
    template <typename ArgTypes...>
    pimpl(ArgTypes&&... args) :
        impl(std::forward<ArgTypes>(args)...)
    {}

    template <typename ArgTypes...>
    void method1(ArgTtypes&&... args) {
        impl.method1(std::forward<ArgTypes>(args)...);
    }

    // continue for each method on "T"
};

---------------------------------------- basic proposal ----------------------------------------

template <typename T>
class pimpl {
    T impl;
public:
    // Using the name of the class forwards the constructors.
    using pimpl = impl;

    // This is the equivalent of the definition of method1 above.
    using method1 = impl.method1;
};

---------------------------------------- extended proposal ----------------------------------------

template <typename T>
class pimpl {
    std::unique_ptr<T> impl;
public:

    // makes method1(...) work as impl->method1(...)
    using method1 = impl->method1;

    // go crazy?
    using method2 = impl->a->b().what;

    // generic forwarding, forwards all non-defined methods
    // option 1:
    using *= impl->*;
    // option 2: more verbose, less cryptic (maybe).
    using auto = impl->auto;

    // possible constructor syntax?
    using pimpl = make_unique<T> -> impl;

    // or just as usual :
    template <ArgTypes...>
    pimpl(ArgTypes&&... args) :
        impl(std::forward<ArgTypes>...)
    {}
};

I'm not sure how hard would it be to create this, but the syntax would not be ambiguous because the left hand side of the using operation would translate to either a method of some field/member or an expression containing "...". This would basically be syntactic sugar to a vardiactic template that forwards all arguments to the "..." site or to the parameter list of the result of the operation.

So if the right side of the using is a type the normal using is used. Otherwise the expression is used to define a method that forwards the arguments.

Victor Bogado da Silva Lins
Reply all
Reply to author
Forward
0 new messages