2

在 c++14 中,如果表达式格式不正确*,则 std::result_of 应该导致 SFINAE。相反,我在下面的最后一个案例中得到了一个编译错误(“二进制表达式的无效操作数”)(即让编译器推断 std::plus<> 的类型)。前三个案例按预期工作。代码和结果如下所示。

#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/apply.hpp>
#include <iostream>
#include <utility>
#include <stdexcept>
#include <functional>

namespace mpl = boost::mpl;


template <typename OP, typename T, typename OP_T = typename mpl::apply<OP, T>::type>
struct apply_if_ok: OP_T {

    template <typename...Args, typename R = std::result_of_t<OP_T(Args...)>>
    R operator()(Args&&...args) const {
        return static_cast<OP_T>(*this)(std::forward<Args>(args)...);
    }
    template <typename...Args>
    auto operator()(...) const {
        // throw std::runtime_error("Invalid arguments");
        return "Invalid arguments";
    }
};


int main() {
    using OP = std::plus<mpl::_>;
    int i = 3;

    auto n1 = apply_if_ok<OP, void>()(1, 2);
    std::cout << "plus (1, 2) = " << n1 << std::endl;

    auto n2 = apply_if_ok<OP, void>()(1, &i);
    std::cout << "plus (1, *) = " << n2 << std::endl;

    auto n3 = apply_if_ok<OP, int>()(&i, &i);
    std::cout << "plus (*, *) = " << n3 << std::endl;

    // auto n4 = apply_if_ok<OP, void>()(&i, &i);
    // std::cout << "plus (*, *) = " << n4 << std::endl;
}

输出:

% c++ -std=c++1y -g -pedantic    sfinae_result_of.cc   -o sfinae_result_of
./sfinae_result_of
plus (1, 2) = 3
plus (1, *) = 0x7fff5e782a80
plus (*, *) = Invalid arguments

% c++ -v
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix

任何关于我做错了什么的指示将不胜感激!

谢谢。

  • 来自cppreference.com。我认为相关的标准参考是 20.10.7.6,对最后一个表条目的评论。
4

1 回答 1

9

这是由libc++ 中的一个错误引起的,实际上我几天前才报告过这个错误。(更新:该错误在主干中修复。)

问题是他们的“菱形函子”实现是不合格的。例如,他们实现std::plus<void>::operator()如下:

template <class _T1, class _T2>
_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
auto operator()(_T1&& __t, _T2&& __u) const
    { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }

什么时候应该

template <class _T1, class _T2>
_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
auto operator()(_T1&& __t, _T2&& __u) const
    -> decltype(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
    { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }

缺少的尾随返回类型意味着两件事:

  1. 他们不再“完美回归”;相反,返回类型是使用规则推导出来的auto,本质上导致它被衰减。尾随返回类型,当其中的表达式格式正确时,等效于返回decltype(auto)
  2. SFINAE 不再适用于表达式_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)。在没有错误的实现中,operator()声明将从重载集中删除,重载解析将失败,然后std::result_of将执行其对 SFINAE 友好的魔法。相反,函数声明被成功实例化,通过重载决议选择,然后当编译器尝试实例化主体以实际推导返回类型时发生硬错误。

您的问题是由#2引起的。

于 2015-02-18T16:54:42.387 回答