c++ - _Pass_fn : where is the constructor?

标签 c++ c++11 templates predicate

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<_Fn> ,但是 _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;
    };

这是如何工作的?

最佳答案

这是 Aggregate initialization 的变体.

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

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

关于c++ - _Pass_fn : where is the constructor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51822521/

相关文章:

c++ - 这个表达式是左值还是右值?

python - django 在模板中显示上下文数据而不通过 url 调用 View

c++ - 相当于 `using` s 的 `template` 别名

c++ - 为什么有条件地编译一个运算符模板会改变另一个运算符的可用性?

c++ - 为什么要用虚表来解析函数调用?

c++ - 扩展类

c++ - 为什么++x 是左值而 x++ 是右值?

c++ - 二维数组的自定义迭代器

c++ - 奇怪的重复模板 - 变体

c++ - 如何让 C++ 模板类调用另一个类的方法?