我正在尝试使用模板元编程实现正弦函数。但是我得到错误'radtest'不是double&的有效模板参数。因为cv资格的冲突。这是代码:
#include <iostream>
using namespace std;
template <double&, int, int> struct
Series;
template <double& rad> struct Sine
{
enum
{
maxterms=10
};
static inline double sin()
{
return (rad) *
Series<(rad), 0, maxterms>::val();
}
};
template <double& rad, int i, int
maxterms> struct Series
{
enum
{
cont = i+1 != maxterms,
nxt1 = (i+1)*cont,
nxtmax = maxterms*cont
};
// uses recursive definition of
// Sine
// sin(x)=x*term(0)
// term(n)=1-
// x*x/(2*n+2)/(2*n+3)*term(n+1)
static inline double val()
{
return 1 - (rad)*
(rad)/(2.0*i+2.0)/(2.0*i+3.0)
* Series<rad * cont, nxt1,
nxtmax>::val();
}
};
#define SineT(rad) Sine<rad>::sin()
constexpr double radtest=0.707;
int main()
{
cout << "Sine of " << radtest
<< " is: " << SineT(radtest);
return 0;
}
可能是什么问题?提前谢谢。
答案 0 :(得分:1)
The problem is that radtest
is const
(implied by constexpr
) so you cannot have double&
parameter of sine
bind to it.
If you try and make it double radtest
(not constexpr) or if you try and make all template parameters const double&
then you get into another problem: you cannot bind a temporary to a reference non-type template parameter. This is explicitly disallowed by the standard:
§ 14.3.2 Template non-type arguments
A template-argument for a non-type template-parameter shall be a converted constant expression (5.20) of the type of the template-parameter. For a non-type template-parameter of reference or pointer type, the value of the constant expression shall not refer to (or for a pointer type, shall not be the address of):
- ...
- (1.2) a temporary object (12.2),
- ...
I personally don't see a way out of this mess. This (using references as template non-type parameters) is really pushing the boundaries of what the C++ template system can do.
What I can recommend is to just create a constexpr
sin function.