首页 > 解决方案 > 导致运行时执行的 constexpr 函子中的成员

问题描述

我正在使用仿函数以下列方式生成编译时计算代码(我为长代码道歉,但这是我发现重现该行为的唯一方法):

#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 变量的值放置在程序集中。

我试图生成一个较小的示例来重现行为,但代码中的微小变化确实在编译时计算。

我以某种方式理解为什么提供constexprlambdas 会有所帮助,但我想了解为什么在这种情况下提供 double 不起作用。理想情况下,我不想提供 lambda,因为它使我的前端更加混乱。

这段代码是一个非常大的代码库的一部分,所以请忽略代码实际计算的内容,我创建了这个示例来展示行为,仅此而已。

const在不改变编译时行为的情况下,为函子提供双精度并将其存储为成员变量的正确方法是什么?

为什么compute()函数中的小修改(例如,其他小的修改也会这样做)确实会产生编译时代码?

我想了解 GCC 提供这些编译时计算的实际条件是什么,因为我正在使用的实际应用程序需要它。

谢谢!

标签: c++lambdaconstexprfunctorcompile-time

解决方案


不确定您的代码何时在运行时执行以及何时在编译时执行,无论如何 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);

推荐阅读