Extension to concepts: Abstract classes to be considered as concepts

83 views
Skip to first unread message

Arthur Cust

unread,
Aug 17, 2017, 2:56:54 PM8/17/17
to SG8 - Concepts
When looking at concepts it reminded me of how abstract classes could be a interface for your objects.
Considering that they are not to be instanced they could be used as concepts:

void fn( const AbstractClass& a) {
...
}

I would say it makes sense to consider abstract class as concept as it is just bunch of functions to be implemented.
As compiler is supposed ensure that all pure functions are implemented, it should be easy to treat it as concept.
In some way it would be even easier to check as you only need to ensure that passed type is derived from Abstract class.

While I'm not entirely sure it is a good idea, the same could be used for non-abstract classes.

template<SomeClass DerivedOrSameClass>
void fn( const DerivedOrSameClass& a) {
...
}

The expected behavior would be that passed object should be derived or belong to SomeClass
In case of Abstract class it can be only derived ofc.

Overall using classes(or only abstract one) can be considered as light version of concepts where user would require only to specify parent class.
With this abstract classes would act as actual interfaces (like in Java or traits in Rust)
User defines bunch of methods in abstract class and just creates template that accepts any derived class in the same way as concepts.

Casey Carter

unread,
Aug 17, 2017, 4:09:53 PM8/17/17
to SG8 - Concepts
You can approximate this using the DerivedFrom concept from the Ranges TS, which you can define yourself as:

template<class T, class U>
concept DerivedFrom = std::is_base_of_v<U, T> && std::is_convertible<std::remove_cv_t<T>*, std::remove_cv_t<U>*>;

Your "concept for types derived from AbstractClass or SomeClass" can then be written as DerivedFrom<AbstractClass> (resp. DerivedFrom<SomeClass>):

template<DerivedFrom<SomeClass> DerivedOrSameClass> 

Arthur Cust

unread,
Aug 17, 2017, 5:01:36 PM8/17/17
to SG8 - Concepts
Oh, I wouldn't think that Ranges TS would introduce such concept.
Then yes, it would help not only with abstract classes, but with regular too.

Though I would still wish to have abstract class itself as a proper concept.
After all it holds no values outside of defining interface which is basically what concept does too.

Thanks for pointing me toward Ranges TS.
Reply all
Reply to author
Forward
0 new messages