【发布时间】:2017-03-24 14:13:53
【问题描述】:
我想创建一个form 类,该类基于其模板参数,为一个operator() 提供一个或多个参数。这是曲线的原型,例如线性、双线性等。
所以,如果表单维度是n,则运算符应该有n 整数参数,所以带有formdim == 1 的线性表单应该有operator()(i) 而formdim == 2 我想要operator()(i, j)。
我认为enable_if 可能会有所帮助,但我对 TMP 并没有深入了解,而且我有一个编译错误:
template<unsigned int formdim>
class form
{
public:
form()
{}
auto operator()(unsigned int j) -> typename std::enable_if<formdim == 1, unsigned int>::type
{
std::cout << "LINEAR" << std::endl;
return 0;
}
// Here I get a compiler error due to the missing type
auto operator()(unsigned int i, unsigned int j) -> typename std::enable_if<formdim == 2, double>::type
{
std::cout << "BILINEAR" << std::endl;
}
};
我怎样才能提供这样的课程?我不需要参数的数量是自动,我可以根据需要手动添加新的运算符...但显然它确实非常酷。
感谢您的帮助!
【问题讨论】:
标签: c++ c++11 templates operator-overloading