C++ Logo

std-proposals

Advanced search

using class namespace -or- Koenig Lookup for arguments and initialisations

From: Marc Mutz <marc.mutz_at_[hidden]>
Date: Mon, 29 Jul 2019 13:35:14 +0300
Hi,

In Qt, we often nest enums and related types in the class which uses
them. This is a fictional example:

    class QFormLayout {
       enum class Role { Label, Editor };
       struct Position { int row; Role role; };
       void setWidgetIn(Position pos, Widget w);
    };

This is all nicely scoped, but not very usable:

    formLayout.setWidgetIn(QFormLayout::Position{0,
QFormLayout::Role::Label}, someLabel);

what one would like to write is this:

    formLayout.setWidgetIn(Position{0, Label}, someLabel});

(not

    formLayout.setWidgetIn({0, Label}, someLabel);

as that would lose the Position name)

wg21.link/p1099 gets us half-way there:

    using enum QFormLayout::Role;

    formLayout.setWidgetIn(QFormLayout::Position{0, Label}, someLabel);

but Position cannot be brought into the current scope without explicitly
aliasing it:

   using Position = QFormLayout::Position;

which I'd consider an option for long functions, but overly verbose for
a short one:

    using enum QFormLayout::Role;
    using Position = QFormLayout::Position;

    formLayout.setWidgetIn(Position{0, Label}, someLabel);

This could be shortned to

    using namespace class QFormLayout; // bring names from QFormLayout
into the current scope
    using class namespace QFormLayout; // different spelling

    formLayout.setWidgetIn(Position{0, Role::Label}, someLabel); //
Role::Label considered acceptable


Another option that avoids the need for using directives and
declarations would be some form of inverse Koenig Lookup: not only find
functions by associated namespaces^Wscopes of their arguments, but also
find argument names in namespaces^Wscopes based on function's
namespaces^Wscopes.

IOW: lookup would have to be performed simultaneously for the functions
and their arguments, and overload resolution would not just resolve the
function, but also its arguments.

Likewise, this could be made legal:

    QFormLayout::Role role = Label; // find Label in 'role's scope

Opinions?

Thanks,
Marc

Received on 2019-07-29 05:37:14