Implementation Section of Class Definition

1,178 views
Skip to first unread message

Andrew Tomazos

unread,
Jul 14, 2019, 3:27:14 AM7/14/19
to std-pr...@isocpp.org
I've been working on a proposal for namespace templates as motivated by what I call the library configuration problem.  One of the things that came up during discussions was a use case around out-of-class definitions in a class template.  I don't currently see a unified solution to both things, however I have come up with an idea for a feature that solves the out-of-class definitions problem.

Let us first state what the out-of-class definition problem is.

When you define a class template you have two choices.  Either you define the members within the class template Java-style:

template<typename T>
class C {
public:
    void f() { /* do stuff */ }
};

or you define the members outside the class template:

template<typename T>
class C {
public:
    void f();
};

template<typename T>
void C<T>::f() { /* do stuff */ }

The problem with defining them inside the class is that it hampers readability as the definitions clutter the interface.

The problem with defining them outside the class is that it requires boilerplate DRY violations to repeat the (potentially lengthy) template head and class name for each definition.

So my idea would be to introduce a new implementation section of a class definition that may only define previously declared member entities.  The section allows the best of both worlds.  You can define the entities in-line in the class, but not clutter the separate interface section:

template<typename T>
class C {
public:
    void f();
implementation:
    void f() { /* do stuff */ }
};

The access level (public, private, protected) of the members defined in the implementation section is taken from their original declaration.

Thoughts?  Is this worth pursuing?

Reply all
Reply to author
Forward
0 new messages