Conflicting return types std :: set iterators

Today is the second compiler error that confused me. Somehow for the code below, gcc complains that the code has a function that returns an iterator that return_iterreturns conflicting types std::_Rb_tree_iterator<const int*>, and then std::_Rb_tree_const_iterator<const int*>, but both of them should not be constant iterators, because the set is not const. Can someone explain why the method, std::end()when calling a non-constant lvalue, returns a const_iterator?

The full code is inserted below.

Note I get this error only when compiling with gcc. This error does not appear when I compile it with clang ( Apple LLVM version 8.0.0 (clang-800.0.38). I am using the gcc versiong++ (GCC) 5.1.0

A related question. Is this the correct use of a forward? Can it be considered std::forwardnormal when you want to use the forwarding link? The reason I call this just in case overloads some methods when the object is an rvalue.


#include <vector>
#include <string>
#include <set>
#include <iostream>
using namespace std;

int global_value = 1;

class LessPtr {
public:

    template <typename PointerComparableOne, typename PointerComparableTwo>
    constexpr auto operator()(PointerComparableOne&& lhs,
                              PointerComparableTwo&& rhs) const {
        return *std::forward<PointerComparableOne>(lhs) <
            *std::forward<PointerComparableTwo>(rhs);
    }

    using is_transparent = std::less<void>::is_transparent;
};

template <typename Container, typename Key>
auto return_iter(Container&& container, Key&& key) {
    if (global_value == 1) {
        return std::forward<Container>(container).lower_bound(std::forward<Key>(key));
    }
    else {
        return std::end(std::forward<Container>(container));
    }
}

void do_stuff(std::set<const int*, LessPtr>& set_ptrs) {
    // auto value = string{"something"};
    auto value = 1;
    auto iter = return_iter(set_ptrs, &value);
    cout << reinterpret_cast<void*>(&iter) << endl;
}

int main() {
    std::set<const int*, LessPtr> set_ptrs;
    do_stuff(set_ptrs);

    return 0;
}

LessPtr somehow it is necessary to cause this error.

+4
source share
1 answer

This is a libstdc ++ error because it std::sethandles transparent comparators. Here is a shorter example:

int main() {
    using S = std::set<const int*, LessPtr>;
    S set_ptrs;

    int i = 0;
    const int ci = 0;
    static_assert(std::is_same<
          decltype(set_ptrs.lower_bound(&ci)), S::iterator>{}, "!"); // OK
    static_assert(std::is_same<
          decltype(set_ptrs.lower_bound(&i)), S::iterator>{}, "!");  // Error

    return 0;
}

, lower_bound(Key const&), iterator. , template <class K> lower_bound(K const&), LessPtr ( ), , libstd++, const_iterator.

set_ptrs const, . 78134

+5

Source: https://habr.com/ru/post/1659085/


All Articles