【发布时间】:2017-10-05 23:48:21
【问题描述】:
目前我实现了两个模板函数,每个都返回一个使用 boost::variant: 包装的模板函数指针:
-
函数fa
typedef boost::variant<&A<int>,&A<double>> A_T; A_T fa(string type) { switch(type){ case "int": return &A<int>; case "double": return &A<double>; default: return &A<int>; } } -
函数fb
typedef boost::variant<&B<int>,&B<double>> B_T; B_T fb(string type) { switch(type){ case "int": return &B<int>; case "double": return &B<double>; default: return &B<int>; } }
我的问题是“我们可以将这两个函数合并为一个模板函数,它以 A 或 B 的函子指针作为模板参数吗?”。我需要这个的原因是因为我可能有两个以上的仿函数,比如 A 和 B。
【问题讨论】:
-
这是什么语言?它看起来不像有效的 C++ 代码。我建议你生成一个minimal reproducible example
-
如果这与您的实际实现类似,您的第一步是尝试通过编译器运行您已有的内容,并找出将出现的几个错误。
标签: c++ templates boost overloading metaprogramming