【问题标题】:_Pass_fn : where is the constructor?_Pass_fn :构造函数在哪里?
【发布时间】:2018-08-13 12:34:58
【问题描述】:
template<class _Fn,
    enable_if_t<!_Pass_functor_by_value_v<_Fn>, int> = 0>
    constexpr _Ref_fn<_Fn> _Pass_fn(_Fn& _Val)
    {   // pass functor by "reference"
    return {_Val};
    }

函数_Pass_fn 返回对象_Ref_fn&lt;_Fn&gt;,但_Ref_fn 没有接受一个参数的构造函数。

template<class _Fx>
    struct _Ref_fn
    {   // pass function object by value as a reference
    template<class... _Args>
        constexpr decltype(auto) operator()(_Args&&... _Vals)
        {   // forward function call operator
        return (_Fn(_STD forward<_Args>(_Vals)...));
        }

    _Fx& _Fn;
    };

这是如何工作的?

【问题讨论】:

  • 这称为聚合初始化。
  • 请注意,此代码中的大多数名称都以下划线开头,后跟大写字母,因此保留供实现使用。不要在你的代码中使用它们。

标签: c++ c++11 templates predicate


【解决方案1】:

这是Aggregate initialization 的变体。

可以在不定义构造函数的情况下使用成员值对简单结构进行大括号初始化:

struct Point {
    int x;
    int y;
};
...
Point p{1,2};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    相关资源
    最近更新 更多