Is it legal for a standard library implementation to specialize a function based on a concept with a child concept?

With the concepts of the C ++ 11 and C ++ 14 library, a valid implementation of the standard C ++ library creates a specialized version of a function created on the basis of the concept to take advantage of the additional functionality of the child concept for optimization that would not be possible with a basic concept, for example, with an InputIterator std::vector constructor and iterators that satisfy the requirements of RandomAccessIterator ?

 // specified by standard vector(InputIt begin, InputIt end, const Allocator& alloc = Allocator()); // is this specialization allowed in an implementation if it provides the same functionality? vector(RandomAccessIt begin, RandomAccessIt end, const Allocator& alloc = Allocator()); 

Here, InputIt is a type that meets the requirements of the InputIterator concept, and RandomAccessIt fulfills the requirements of RandomAccessIterator . It is noteworthy that in this concept there is no requirement to find the difference between the two iterators, while the concept of her great-grandmother RandomAccessIterator requires

 It a,b; It::difference_type c = a - b; 

. Finding the difference between the two iterators would be useful for the InputIterator constructor for std::vector in cases where RandomAccessIterator also a concept performed by the provided iterators, since this would allow the implementation to pre-allocate the necessary space by the final vector, instead of changing it a little times during construction.

I see that this is true, due to the standard random use of as-if rules, for example, with covariant return types for virtual functions in inheritance hierarchies. However, there are fairly clear differences between the situations, so I can also see that the logic of the covariant return types may not necessarily carry over into this situation.


To repeat: can a valid implementation of the C ++ standard library create a specialized version of a function created on the basis of the concept in order to take advantage of the additional functionality of the child concept for optimization that would not be possible only with the basic concept?


Note: I did not mention this question of C ++ concepts , because, as far as I can tell, this tag is for Concepts-Lite and Concepts TS, and this question concerns library concepts in C ++ 11 and C ++ 14

+7
c ++ language-lawyer c ++ 11 c ++ 14
source share
1 answer

It is not possible to determine the external constructor that you called with the given signature in C ++.

Thus, the exact details of the vector signature can vary according to the as-if rule, if all the called constructors can be called, the behavior you get satisfies the documented functions, and any constructor arguments that were not valid remain invalid (because the external code can do SFINAE testing to determine if a given set of arguments will build std::vector ).

As noted by the OP and the comments, many standard library implementations simply use label dispatching to migrate to more efficient, more specialized versions.

It would be interesting to have an answer outside the context of the constructor, where you can find functions that have certain signatures (and not just compatibility), and you can distinguish between different functions. I don’t know the answer here, but I am under the (unapproved) impression that functions that do not correspond to the exact signatures described in the standard are actually found sometimes in std libraries.

+2
source share

All Articles