Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

cpp – alias for member functions` return types

I’ve been watching this CppCon talk where the speaker was talking about writing classes resistant to future changes. He provided the following code as an example (that I show here abridged):

template< typename Type, size_t Cap >
class FixedVector final
{
    using iterator = Type*;
    
    iterator begin();
}

My question is, basically the following – Does he use type aliasing here only because the type itself is already provided by the user of the class (and is a priory know to them)? I mean, if that was not the case, then there is not way the user can use the aliased return type, right?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The point they are trying to make is if they had

template< typename Type, size_t Cap >
class FixedVector final
{
    Type* begin();
}

and later they decide that instead of a Type*, they want to use a my_custom_iterator<Type>, then they need to make that change to all places that use Type*. By using

class FixedVector final
{
    using iterator = Type*;
    
    iterator begin();
}

if you want to make that same change it is as simple as changing

using iterator = Type*;

to be

using iterator = my_custom_iterator<Type>;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading