将std::endl强制转换为函数指针
Cast std::endl to a function pointer
这是模板函数如何选择参数的后续问题?
@Kerrek SB提出了以下解决方案:
Func(static_cast<std::ostream&(&)(std::ostream&)>(std::endl));
我还发现以下代码也适用于我:
Func(static_cast<std::ostream&(*)(std::ostream&)>(std::endl));
问题>首选哪种方法?
我会这样做:
template<class Sig>
struct reference_to_function {};
template<class R, class... Args>
struct reference_to_function<R(Args...)> {
using type = R(&)(Args...);
};
template<class Sig>
using sig_t = typename reference_to_function<Sig>::type;
template<class Sig>
sig_t<Sig> pick_signature( sig_t<Sig> f ) { return f; }
然后:
Func( pick_signature<std::ostream&(std::ostream&)>( std::endl ) );
这使它明确了我想做什么,而不是static_cast
。
也许可以制作第二个版本,它采用类类型和签名,并对方法进行签名。(我不知道如何让它在方法中透明地工作,我认为理论上也不可能)。
相关文章:
- 如何使用Rcpp将R函数转换为C++函数
- C++函数转换为 C# 函数
- 将 C 函数转换为 C++ 以检查数字是否有效
- C++:从重载函数转换为 std::function
- 将 C 函数转换为C++语言
- C++隐式构造函数转换,后跟类型向上转换
- 将 lambda 函数转换为具有混合 lambda 引入器和参数列表的函子结构
- 通过 Boost Python 将 Python 函数转换为 C++,用作回调
- 如何将 MATLAB 图像处理库内置函数转换为 MATLAB 编码器代码生成不支持的 C++?
- 如何将函数转换为 lambda 函数
- 自定义函数转换错误?
- 将 C# 哈希函数转换为C++
- 将 lambda 函数转换为另一个编译单元中的普通函数会缩短编译时间吗?
- 加快 R 性能或将 R 函数转换为C++函数
- 将成员函数转换为指向成员函数的指针
- 将MATLAB炒作函数转换为C
- Rcpp - 用二进制函数转换数字向量?
- C lambda函数转换误差
- 此语法中的构造函数转换错误
- 将函数转换为find_if lambda