解决函数模板部分专用化问题
work around function template partial specialization
我可以做这样的事情吗?
template <int N, int Y>
constexpr int f(char *(&arr)[Y])
{
return N * f<N - 1, Y - 1>();
}
// Y must appear here too because it's used in parameters
// and if I remove this I get "no matching function" error
template <int N, int Y>
constexpr int f<1, 1>(char *(&arr)[Y]) // run this when Y == 0 and N == 0
{
return 1;
}
int main()
{
size_t x = f<4,4>();
printf("x = %zun", x);
}
通常有
三种用于部分函数专业化的解决方案(尽管您似乎具有完全专业化):
std::enable_if / std::disable_if
例如:
template<int X, int Y>
typename std::enable_if<X == 0 && Y == 0>::type
int f();
将实现委托给结构(对我来说似乎是最干净的):
template<int X, int Y>
struct f_impl {
int eval();
};
template<int X, int Y>
int f() {
return f_impl<x,Y>::eval();
}
然后部分专业化实施
template<>
struct f_impl<0,0>;
将模板参数转发为魔术函数参数,然后实现重载:
template<int X, int Y>
int f(mpl::int_<X>, mpl::int_<Y>);
int f(mpl::int_<0>, mpl::int_<0>);
template<int X, int Y>
int f() {
f(mpl::int_<X>(), mpl::int_<Y>());
}
有关更多元编程帮助程序,请参阅boost::mpl
,例如包装类型的mpl::identity
:http://www.boost.org/doc/libs/1_55_0b1/libs/mpl/doc/index.html
相关文章:
- 警告处理为错误这里有什么问题
- .cpp和.h文件中的模板专用化声明
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- C++部分模板专用化问题
- C++隐式/显式模板方法专用化问题
- C++ GCC 的部分模板专用化问题
- 专用纯虚拟模板函数(未定义引用)的另一个问题
- 嵌套类模板专用化问题
- 显式模板专用化和依赖名称查找问题
- 模板专用化和enable_if问题
- 别名模板专用化问题
- 模板专用化问题
- 部分模板专用化有什么问题
- g++中的显式模板专用化导致问题
- C++结构成员模板函数的显式专用化 - 这是一个Visual Studio问题吗?
- 解决函数模板部分专用化问题