How do __glibcxx_function_requires and __glibcxx_requires_valid_range macros work?

template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
inline _Tp
accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
{
    // concept requirements
    __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
    __glibcxx_requires_valid_range(__first, __last);

    for (; __first != __last; ++__first)
        __init = __binary_op(__init, *__first);
    return __init;
}

I examined the definition of the accumulation function in the stl library. Here I found two macros __glibcxx_function_requires and __glibcxx_requires_valid_range, which are defined as follows:

#define __glibcxx_function_requires(...)
# define __glibcxx_requires_valid_range(_First,_Last)

Please can you explain to me how they work and what they do?

+5
source share
1 answer

When _GLIBCXX_CONCEPT_CHECKSdefined, it is.

#define __glibcxx_function_requires(...)                                 \
         __gnu_cxx::__function_requires< __gnu_cxx::__VA_ARGS__ >();

So your published code:

__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)

Solves the following:

__gnu_cxx::__function_requires< _InputIteratorConcept<_InputIterator> >();

Which one is related to:

void (_InputIteratorConcept<_InputIterator>::*__x)() _IsUnused = &_InputIteratorConcept<_InputIterator>::__constraints;

This creates an instance _InputIteratorConcept<_InputIterator>::__constraintsthat it uses typedefto break compilation when it _InputIteratordoesn't look like an iterator.

__glibcxx_requires_valid_range . ( ), , __last __first

+5

All Articles