4

我有一个类似容器的类,其方法与std::apply. 我想用const限定符重载此方法,但是当我尝试使用通用 lambda 调用此方法时,我从std::invoke_result_t. 我std::invoke_result_t用来推断方法的返回值以及对参数执行 SFINAE 检查。

#include <type_traits>
#include <utility>

template <typename T>
class Container
{
public:
    template <typename F>
    std::invoke_result_t<F, T &> apply(F &&f)
    {
        T dummyValue;
        return std::forward<F>(f)(dummyValue);
    }

    template <typename F>
    std::invoke_result_t<F, const T &> apply(F &&f) const
    {
        const T dummyValue;
        return std::forward<F>(f)(dummyValue);
    }
};

int main()
{
    Container<int> c;
    c.apply([](auto &&value) {
        ++value;
    });
    return 0;
}

使用 Clang 6.0 编译时的错误消息:

main.cc:27:9: error: cannot assign to variable 'value' with const-qualified type 'const int &&'
        ++value;
        ^ ~~~~~
type_traits:2428:7: note: in instantiation of function template specialization 'main()::(anonymous class)::operator()<const int &>' requested here
      std::declval<_Fn>()(std::declval<_Args>()...)
      ^
type_traits:2439:24: note: while substituting deduced template arguments into function template '_S_test' [with _Fn = (lambda at main.cc:26:13), _Args = (no value)]
      typedef decltype(_S_test<_Functor, _ArgTypes...>(0)) type;
                       ^
type_traits:2445:14: note: in instantiation of template class 'std::__result_of_impl<false, false, (lambda at main.cc:26:13), const int &>' requested here
    : public __result_of_impl<
             ^
type_traits:2831:14: note: in instantiation of template class 'std::__invoke_result<(lambda at main.cc:26:13), const int &>' requested here
    : public __invoke_result<_Functor, _ArgTypes...>
             ^
type_traits:2836:5: note: in instantiation of template class 'std::invoke_result<(lambda at main.cc:26:13), const int &>' requested here
    using invoke_result_t = typename invoke_result<_Fn, _Args...>::type;
    ^
main.cc:16:10: note: in instantiation of template type alias 'invoke_result_t' requested here
    std::invoke_result_t<F, const T &> apply(F &&f) const
         ^
main.cc:26:7: note: while substituting deduced template arguments into function template 'apply' [with F = (lambda at main.cc:26:13)]
    c.apply([](auto &&value) {
      ^
main.cc:26:23: note: variable 'value' declared const here
    c.apply([](auto &&value) {
               ~~~~~~~^~~~~

我不确定是否对std::invoke_result_tSFINAE 友好,但我认为这不是问题所在,因为我尝试用尾随返回类型替换它,例如:

auto apply(F &&f) const -> decltype(std::declval<F>()(std::declval<const T &>()))

并得到了类似的错误:

main.cc:27:9: error: cannot assign to variable 'value' with const-qualified type 'const int &&'
        ++value;
        ^ ~~~~~
main.cc:16:41: note: in instantiation of function template specialization 'main()::(anonymous class)::operator()<const int &>' requested here
    auto apply(F &&f) const -> decltype(std::declval<F>()(std::declval<const T &>()))
                                        ^
main.cc:26:7: note: while substituting deduced template arguments into function template 'apply' [with F = (lambda at main.cc:26:13)]
    c.apply([](auto &&value) {
      ^
main.cc:26:23: note: variable 'value' declared const here
    c.apply([](auto &&value) {
               ~~~~~~~^~~~~

问题:

  1. 为什么会这样?更准确地说,为什么 lambda 的主体在重载决议期间被实例化,似乎是什么?
  2. 我该如何解决它?
4

2 回答 2

5

所以重载决议在这里有点愚蠢。

它没有说“好吧,如果不const apply工作,我永远不会打电话const apply,所以我不会费心考虑它”。

相反,重载决议会评估每个可能的候选者。然后它会消除那些遭受替换失败的人。只有这样,它才会对候选人进行排序并挑选一个。

所以这两个都F代替了它们:

template <typename F>
std::invoke_result_t<F, T &> apply(F &&f)

template <typename F>
std::invoke_result_t<F, const T &> apply(F &&f) const

我移走了他们的尸体。

现在,当您将 lambda 类型传递F给这些时会发生什么?

嗯,lambdas 有一个auto返回类型的等价物。为了找出传递某些东西时的实际返回类型,编译器必须检查 lambda 的主体。

在检查函数体(或 lambdas)时,SFINAE 不起作用。这是为了使编译器的工作更容易(因为 SFINAE 对编译器来说非常困难,让他们必须编译任意代码并遇到任意错误然后回滚是一个巨大的障碍)。

我们可以避免用这个来实例化 lambda 的主体:

[](auto &&value) -> void { /* ... */ }

在你这样做之后,两个重载apply

template <typename F>
std::invoke_result_t<F, T &> apply(F &&f)

template <typename F>
std::invoke_result_t<F, const T &> apply(F &&f) const

可以评估返回值(它只是void),我们得到:

template <typename F=$lambda$>
void apply(F &&f)

template <typename F=$lambda$>
void apply(F &&f) const

现在,请注意apply const仍然存在。如果您调用apply const,您将得到由实例化该 lambda 主体引起的硬错误。

如果你希望 lambda 本身对 SFINAE 友好,你应该这样做:

#define RETURNS(...) \
  noexcept(noexcept(__VA_ARGS__)) \
  -> decltype(__VA_ARGS__) \
  { return __VA_ARGS__; }

[](auto &&value) RETURNS(++value)

请注意,此 lambda 略有不同,因为它返回对值的引用。我们可以通过以下方式避免这种情况:

[](auto &&value) RETURNS((void)++value)

现在 lambda 既对 SFINAE 友好,并且与您的原始 lambda 具有相同的行为,并且您的原始程序在此更改后按原样编译。

这样做的副作用是constSFINAE 现在从重载解决方案中消除了非应用。这反过来又使它对 SFINAE 友好。

有一个提议要接受RETURNS并重命名它=>,但最后我检查它不被接受。

于 2018-11-16T20:11:57.353 回答
5

Lambda 已推导出返回类型,除非您明确指定返回类型。因此,std::invoke_result_t必须实例化主体以确定返回类型。此实例化不在直接上下文中,并导致硬错误。

您可以通过编写以下代码来编译您的代码:

[](auto &&value) -> void { /* ... */ }

在这里,lambda 的主体在 的主体之前不会被实例化apply,并且您很清楚。

于 2018-11-16T20:00:12.750 回答