int p在lpNorm p中不是常数表达式。 [英] int p is not a constant expression in lpNorm<p>
本文介绍了int p在lpNorm p中不是常数表达式。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了此函数
template <typename T>
double norm(const T & v, const int p) {
return v.template lpNorm<p>();
}
但它不起作用并给出错误:
but it doesn't work and gives the error:
error: 'p' is not a constant expression
return v.template lpNorm<p>();
^
我似乎认为编译器期望 p
在编译时是已知的,而我的 p
是动态的。
I seem to think that the compiler expects p
to be known at compile time whereas my p
is dynamic.
可能相关:
如何解决此问题?
推荐答案
您可以使用 p
的有限范围来部分执行此操作。对于 lpNorm
操作通常就足够了。
You can partially do it with a limited range of p
. For lpNorm
operation it is usually enough.
您已经知道您需要一个编译时常量,如下所示:模板参数。但是,由于 p
通常用于较小的范围(0,1,2,inf),因此您可以使用以下技巧使其适用于以下子集:
You've already known that you need a compile-time constant as the template parameter. However as p
is usually used in a small range (0,1,2,inf), you could use the following trick to make it work for a small sub-set of commonly used int
values.
template<typename T>
double norm(const T & v, const int p) {
switch (p) {
case 0:
return v.template lpNorm<0>();
break;
case 1:
return v.template lpNorm<1>();
break;
case 2:
return v.template lpNorm<2>();
break;
case Eigen::Infinity:
return v.template lpNorm<Eigen::Infinity>();
break;
default:
break;
}
}
这篇关于int p在lpNorm p中不是常数表达式。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文