samedi 28 mai 2016

What is begin() == end()?

array.zero-2 says:

In the case that N == 0, begin() == end() == unique value. The return value of data() is unspecified.

What does this unique value mean? Does it mean that the result of the expression begin() == end() is a unique value? I don't see how that makes any sense, as a bool can only be true or false, which isn't very unique. I ran the following tests:

#include <array>
#include <iostream>
#include <iomanip>
#include <type_traits>

int main()
{
    std::array<int, 0> test;
    auto b = test.begin();
    auto e = test.end();
    std::cout << std::boolalpha 
              << (test.begin() == test.end())
              << std::is_same<bool, decltype(test.begin() == test.end())>::value
              << std::is_same<bool, decltype(b == e)>::value;
}

It all outputs true as expected. Then I wondered maybe it would be different for a non zero-sized array:

int main()
{
    std::array<int, 0> test;
    std::array<int, 1> test2;
    std::cout << std::boolalpha
              << std::is_same<
                    decltype(test.begin() == test.end()),
                    decltype(test2.begin() == test2.end())
                >::value
              << std::is_same<
                    decltype(test.begin()),
                    decltype(test2.begin())
                >::value;
}

Again all true. So what does "unique value" mean? What has a unique value?

Aucun commentaire:

Enregistrer un commentaire