尝试将指向成员函数的指针用作 ranges::find() 的投影时出现编译错误

compile error when trying to use pointer-to-member function as projection to ranges::find()

我想通过访问器在成员中搜索具有特定值的元素的输入范围。

range-v3 文档是...thin,但是有 AFAIK 的 等来源,2 个主要 range-v3 开发人员表示这种事情应该 Just Work,尽管 sort 而不是 find.

给定代码,使用 g++ -std=c++17 针对 ericniebler/range-v3 版本 range-v3-0.10.0 编译,使用 g++.exe (Rev2, Built by MSYS2 project) 9.3.0:

#include <range/v3/algorithm/find.hpp>
#include <vector>

auto
main() -> int
{
    struct S {
        int i{};
        auto get_i() const { return i; }
    };

    auto const ss = std::vector<S>(10);
    ranges::find(ss, 1, &S::get_i);
    return 0;
}

我收到一大堆错误:

test.cpp: In function 'int main()':
test.cpp:14:31: error: no match for call to '(const ranges::find_fn) (const std::vector<main()::S>&, int, int (main()::S::*)() const)'
   14 |  ranges::find(ss, 1, &S::get_i);
      |                               ^
In file included from FOO/include/range-v3/range/v3/range_fwd.hpp:24,
                 from FOO/include/range-v3/range/v3/algorithm/find.hpp:18,
                 from test.cpp:1:
FOO/include/range-v3/range/v3/detail/config.hpp:618:27: note: candidate: 'template<class I, class S, class V, class P> constexpr concepts::return_t<I, typename std::enable_if<(((input_iterator<I> && sentinel_for<S, I>) && indirect_relation<ranges::equal_to, typename ranges::detail::select_projected_<P>::apply<I>, const V*>) && concepts::detail::CPP_true(concepts::detail::Nil{})), void>::type> ranges::find_fn::operator()(I, S, const V&, P) const'
  618 | #define RANGES_FUNC(NAME) operator() RANGES_FUNC_CONST_ /**/
      |                           ^~~~~~~~
FOO/include/range-v3/range/v3/algorithm/find.hpp:47:24: note: in expansion of macro 'RANGES_FUNC'
   47 |         constexpr auto RANGES_FUNC(find)(I first, S last, V const & val, P proj = P{})
      |                        ^~~~~~~~~~~
FOO/include/range-v3/range/v3/detail/config.hpp:618:27: note:   template argument deduction/substitution failed:
  618 | #define RANGES_FUNC(NAME) operator() RANGES_FUNC_CONST_ /**/
      |                           ^~~~~~~~
FOO/include/range-v3/range/v3/algorithm/find.hpp:47:24: note: in expansion of macro 'RANGES_FUNC'
   47 |         constexpr auto RANGES_FUNC(find)(I first, S last, V const & val, P proj = P{})
      |                        ^~~~~~~~~~~
In file included from FOO/include/range-v3/range/v3/iterator/access.hpp:22,
                 from FOO/include/range-v3/range/v3/iterator/concepts.hpp:30,
                 from FOO/include/range-v3/range/v3/algorithm/find.hpp:22,
                 from test.cpp:1:
FOO/include/range-v3/std/detail/associated_types.hpp: In substitution of 'template<bool B, class T> using enable_if_t = typename ranges::detail::enable_if::apply<T> [with bool B = ranges::readable<std::vector<main()::S> >; T = std::vector<main()::S>]':
FOO/include/range-v3/range/v3/iterator/concepts.hpp:561:19:   required by substitution of 'template<class I> using apply = ranges::detail::enable_if_t<(bool)(readable<I>), I> [with I = std::vector<main()::S>]'
FOO/include/range-v3/range/v3/algorithm/find.hpp:48:15:   required by substitution of 'template<class I, class S, class V, class P> constexpr concepts::return_t<I, typename std::enable_if<(((input_iterator<I> && sentinel_for<S, I>) && indirect_relation<ranges::equal_to, typename ranges::detail::select_projected_<P>::apply<I>, const V*>) && concepts::detail::CPP_true(concepts::detail::Nil{})), void>::type> ranges::find_fn::operator()(I, S, const V&, P) const [with I = std::vector<main()::S>; S = int; V = int (main()::S::*)() const; P = ranges::identity]'
test.cpp:14:31:   required from here
FOO/include/range-v3/std/detail/associated_types.hpp:73:15: error: no class template named 'apply' in 'struct ranges::detail::enable_if<false>'
   73 |         using enable_if_t = typename enable_if<B>::template apply<T>;
      |               ^~~~~~~~~~~
In file included from FOO/include/range-v3/range/v3/range_fwd.hpp:24,
                 from FOO/include/range-v3/range/v3/algorithm/find.hpp:18,
                 from test.cpp:1:
FOO/include/range-v3/range/v3/detail/config.hpp:618:27: note: candidate: 'template<class Rng, class V, class P> constexpr concepts::return_t<typename ranges::detail::if_then<forwarding_range_<R> >::apply<decltype (ranges::_::begin(declval<Rng&>())), ranges::dangling>, typename std::enable_if<((input_range<Rng> && indirect_relation<ranges::equal_to, typename ranges::detail::select_projected_<P1>::apply<decltype (ranges::_::begin(declval<Rng&>()))>, const V*>) && concepts::detail::CPP_true(concepts::detail::Nil{})), void>::type> ranges::find_fn::operator()(Rng&&, const V&, P) const'
  618 | #define RANGES_FUNC(NAME) operator() RANGES_FUNC_CONST_ /**/
      |                           ^~~~~~~~
FOO/include/range-v3/range/v3/algorithm/find.hpp:60:24: note: in expansion of macro 'RANGES_FUNC'
   60 |         constexpr auto RANGES_FUNC(find)(Rng && rng, V const & val, P proj = P{})
      |                        ^~~~~~~~~~~
FOO/include/range-v3/range/v3/detail/config.hpp:618:27: note:   template argument deduction/substitution failed:
  618 | #define RANGES_FUNC(NAME) operator() RANGES_FUNC_CONST_ /**/
      |                           ^~~~~~~~
FOO/include/range-v3/range/v3/algorithm/find.hpp:60:24: note: in expansion of macro 'RANGES_FUNC'
   60 |         constexpr auto RANGES_FUNC(find)(Rng && rng, V const & val, P proj = P{})
      |                        ^~~~~~~~~~~
In file included from FOO/include/range-v3/range/v3/iterator/access.hpp:22,
                 from FOO/include/range-v3/range/v3/iterator/concepts.hpp:30,
                 from FOO/include/range-v3/range/v3/algorithm/find.hpp:22,
                 from test.cpp:1:
FOO/include/range-v3/std/detail/associated_types.hpp: In substitution of 'template<bool B, class T> using enable_if_t = typename ranges::detail::enable_if::apply<T> [with bool B = ranges::indirectly_regular_unary_invocable<int (main()::S::*)() const, __gnu_cxx::__normal_iterator<const main()::S*, std::vector<main()::S> > >; T = ranges::detail::projected_<__gnu_cxx::__normal_iterator<const main()::S*, std::vector<main()::S> >, int (main()::S::*)() const>]':
FOO/include/range-v3/range/v3/iterator/concepts.hpp:552:19:   required by substitution of 'template<class Proj> template<class I> using apply = ranges::detail::enable_if_t<(bool)(indirectly_regular_unary_invocable<Proj, I>), ranges::detail::projected_<I, Proj> > [with I = __gnu_cxx::__normal_iterator<const main()::S*, std::vector<main()::S> >; Proj = int (main()::S::*)() const]'
FOO/include/range-v3/range/v3/algorithm/find.hpp:61:15:   required by substitution of 'template<class Rng, class V, class P> constexpr concepts::return_t<typename ranges::detail::if_then<forwarding_range_<R> >::apply<decltype (ranges::_::begin(declval<Rng&>())), ranges::dangling>, typename std::enable_if<((input_range<Rng> && indirect_relation<ranges::equal_to, typename ranges::detail::select_projected_<P1>::apply<decltype (ranges::_::begin(declval<Rng&>()))>, const V*>) && concepts::detail::CPP_true(concepts::detail::Nil{})), void>::type> ranges::find_fn::operator()(Rng&&, const V&, P) const [with Rng = const std::vector<main()::S>&; V = int; P = int (main()::S::*)() const]'
test.cpp:14:31:   required from here
FOO/include/range-v3/std/detail/associated_types.hpp:73:15: error: no class template named 'apply' in 'struct ranges::detail::enable_if<false>'
   73 |         using enable_if_t = typename enable_if<B>::template apply<T>;
      |               ^~~~~~~~~~~

shell returned 1

将投影设为 lambda...

ranges::find( ss, 1, [](auto const& s){ return s.get_i(); } );

...有效但似乎浪费了打字时间。


直接引用数据成员...

ranges::find(ss, 1, &S::i);

...可行,但如果它应该封装在 const getter、转换器等之后是不可能的


我做错了什么?我不能使用指向成员的指针-函数作为投影吗?是故意的吗?


编辑clang++(也在 MSYS2 上) 在这里工作。所以我想这一定是 g++ 中的错误。去 Bugzilla 我去... 编辑: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94973

这是由于 Windows 上的 GCC 默认为 -fms-extensions 以与 Microsoft 编译器兼容,并且引入了一个非标准扩展,该扩展在指向成员数据的指针与指向成员数据的指针之间引入了歧义- 写成 instance.*member.

的函数

我们可以通过 -fno-ms-extensions 来阻止它并成功编译这样的代码,直到 GCC 删除那个特定的扩展,这在现在看来是不必要和无用的。

Jonathan 在 my GCC bug 94973 上回答了这个问题:


Jonathan Wakely 2020-05-06 16:26:58 UTC

Aha, the same problem happens on linux if I compile with -fms-extensions

This is the old MS extension that causes x.*f to be accepted when f is a pointer to member function, which should only be valid when used as (x.*f)().

That causes ranges::invoke to think that the projection is a pointer to data member, when actually it's a pointer to member function.

See also PR 94771 comment 4.

Jason, do we want to disable that extension in SFINAE contexts?

Jonathan Wakely 2020-05-06 16:47:42 UTC

They're on by default for mingw, for compatibility with the MS compiler (but in this case it seems the relevant extension is ancient history).