我正在使用仿函数以下列方式生成编译时计算代码(我为长代码道歉,但这是我发现重现行为的唯一方法):
#include <array>
#include <tuple>
template <int order>
constexpr auto compute (const double h)
{
std::tuple<std::array<double,order>,
std::array<double,order> > paw{};
auto xtab = std::get<0>(paw).data();
auto weight = std::get<1>(paw).data();
if constexpr ( order == 3 )
{
xtab[0] = - 1.0E+00;
xtab[1] = 0.0E+00;
xtab[2] = 1.0E+00;
weight[0] = 1.0 / 3.0E+00;
weight[1] = 4.0 / 3.0E+00;
weight[2] = 1.0 / 3.0E+00;
}
else if constexpr ( order == 4 )
{
xtab[0] = - 1.0E+00;
xtab[1] = - 0.447213595499957939281834733746E+00;
xtab[2] = 0.447213595499957939281834733746E+00;
xtab[3] = 1.0E+00;
weight[0] = 1.0E+00 / 6.0E+00;
weight[1] = 5.0E+00 / 6.0E+00;
weight[2] = 5.0E+00 / 6.0E+00;
weight[3] = 1.0E+00 / 6.0E+00;
}
for (auto & el : std::get<0>(paw))
el = (el + 1.)/2. * h ;
for (auto & el : std::get<1>(paw))
el = el/2. * h ;
return paw;
}
template <std::size_t n>
class Basis
{
public:
constexpr Basis(const double h_) :
h(h_),
paw(compute<n>(h)),
coeffs(std::array<double,n>())
{}
const double h ;
const std::tuple<std::array<double,n>,
std::array<double,n> > paw ;
const std::array<double,n> coeffs ;
constexpr double operator () (int i, double x) const
{
return 1. ;
}
};
template <std::size_t n,std::size_t p,typename Ltype,typename number=double>
class Functor
{
public:
constexpr Functor(const Ltype L_):
L(L_)
{}
const Ltype L ;
constexpr auto operator()(const auto v) const
{
const auto l = L;
// const auto l = L();
std::array<std::array<number,p+1>,p+1> CM{},CM0{},FM{};
const auto basis = Basis<p+1>(l);
typename std::remove_const<typename std::remove_reference<decltype(v)>::type>::type w{};
for (auto i = 0u; i < p + 1; ++i)
CM0[i][0] += l;
for (auto i = 0u ; i < p+1 ; ++i)
for (auto j = 0u ; j < p+1 ; ++j)
{
w[i] += CM0[i][j]*v[j];
}
for (auto b = 1u ; b < n-1 ; ++b)
for (auto i = 0u ; i < p+1 ; ++i)
for (auto j = 0u ; j < p+1 ; ++j)
{
w[b*(p+1)+i] += CM[i][j]*v[b*(p+1)+j];
w[b*(p+1)+i] += FM[i][j]*v[(b+1)*(p+1)+j];
}
return w ;
}
};
int main(int argc,char *argv[])
{
const auto nel = 4u;
const auto p = 2u;
std::array<double,nel*(p+1)> x{} ;
constexpr auto L = 1.;
// constexpr auto L = [](){return 1.;};
const auto A = Functor<nel,p,decltype(L)>(L);
const volatile auto y = A(x);
return 0;
}
我用GCC 8.2.0编译带有标志:
-march=native -std=c++1z -fconcepts -Ofast -Wa,-adhln
在查看生成的程序集时,计算正在运行时执行。
如果我更改了紧接在下面的行注释的两行,我发现代码确实在编译时执行,并且只是volatile变量的值放在程序集中。
我试图生成一个更小的示例来重现行为,但代码中的小变化确实在编译时计算。
我不明白地理解为什么提供constexpr
lambdas会有所帮助,但我想理解为什么在这种情况下提供double不起作用。理想情况下,我不想提供lambdas,因为它让我的前端变得更加混乱。
这段代码是一个非常大的代码库的一部分,所以请忽略代码实际计算的内容,我创建了这个示例以显示行为,仅此而已。
在不改变编译时行为的情况下,为仿函数提供双精度并将其存储为const
成员变量的正确方法是什么?
为什么compute()
函数中的小修改(例如,其他小的修改也这样做)确实产生编译时代码?
我想了解GCC提供这些编译时计算的实际条件是什么,因为我正在使用的实际应用程序需要它。
谢谢!
无法理解代码执行时的运行时和编译时的执行时间,无论如何C ++语言的规则(不仅是g ++而忽略了as-if规则)是constexpr
函数
constexpr
变量的初始化,非类型模板参数,C样式数组维度,static_assert()
测试)如果你有兴趣
const volatile auto y = A(x);
在我看来,我们处于灰色区域,编译器可以选择是否计算y
编译时或运行时的初始值。
如果你想要一个y
初始化编译时,我想你可以获得这个定义它(以及前面的变量)constexpr
constexpr auto nel = 4u;
constexpr auto p = 2u;
constexpr std::array<double,nel*(p+1)> x{} ;
constexpr auto L = 1.;
// constexpr auto L = [](){return 1.;};
constexpr auto A = Functor<nel,p,decltype(L)>(L);
constexpr volatile auto y = A(x);
for (auto i = 0u; i < p + 1; ++i)
CM0[i][0] += l;
当l
是无状态lambda类型时,它将l
转换为函数类型,然后转换为bool(整数类型)。允许这两步转换,因为只有一个是“用户定义的”。
此转换始终生成1,并且不依赖于l
的状态。