将std::forward作为默认参数传递
Passing std::forward as a default argument?
考虑以下以函数为实参的函数:
template <class Function = std::plus<int> >
void apply(Function&& f = Function());
这里std::plus<int>
是应用的默认函数。std::plus<int>
是一个函数对象,并且都运行良好。
现在,我想传递std::forward<int>
作为默认参数。std::forward<int>
不是一个函数对象,这是一个函数指针。怎么做?
template <class Function = /* SOMETHING */ >
void apply(Function&& f = /* SOMETHING */);
指向std::forward<int>
的函数指针类型为int &&(*)(int &)
。所以你的函数应该是这样的:
template<class T = int &&(*)(int &)>
void apply(T &&t = &std::forward<int>);
看一下std::forward是如何声明的:http://en.cppreference.com/w/cpp/utility/forward
我认为这将工作:
template <class Function = decltype(&std::forward<int>)>
void apply(Function&& f = &std::forward<int>);
编辑:实际上,也许不是。你最好重载它:
void apply() {
apply(&std::forward);
}
相关文章:
- 有条件地将默认参数传递给函数(使用"?"运算符)
- 将私有数据成员作为默认参数传递给该类的公共方法时出错
- 在一行中将默认类型值上的指针作为参数传递
- 如何将值传递给其参数(默认参数)
- 为什么将类作为参数传递需要默认构造函数
- C++中出现意外的编译错误:将默认值传递给函数参数
- 如何从类构造函数传递值C++作为此类方法的默认参数?
- C++ 在C++中通过引用将枚举作为默认参数传递时出错
- 将非静态数据成员作为默认参数传递给方法
- 在构造函数 QT 创建器(继承类)中传递默认参数
- 将带有默认值的函数作为忽略它们的参数传递
- 如何传递 std::shared_ptr <PureVirtualClass>的默认参数
- 将新对象作为默认参数传递给构造函数
- 传递将默认参数值作为模板参数的函数
- (C++)将"this"作为默认参数传递给静态方法
- 将类成员 (Map) 的指针作为默认参数传递给另一个类方法
- 将变量名作为字符串传递给具有默认参数的函数
- 如何将其他类型的默认参数传递给参数
- 将std::map作为默认参数传递
- 将std::forward作为默认参数传递