c++ - 导致运行时执行的 constexpr 仿函数中的成员

标签 c++ lambda constexpr functor compile-time

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

#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 lambda 有帮助,但我想了解为什么在这种情况下提供 double 不起作用。理想情况下,我不想提供 lambda,因为它会使我的前端更加困惑。

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

在不改变编译时行为的情况下,向仿函数提供 double 并将其存储为 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);

关于c++ - 导致运行时执行的 constexpr 仿函数中的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54749222/

相关文章:

c++ - 使用 gmock 模拟 pthread_create

c++ - 等待 QMutex 断言

c++ - 我应该通过 const 引用传递一个 lambda。

javascript - 嵌入传递 lambda 函数作为回调参数

c++ - constexpr 求值分支/constexpr 重载

c++ - Constexpr 和 SSE 内在函数

c++ - 保存 tuple_cat 的结果

c++ - 为什么我可以在方法中使用 'this'

Java 8 : Target typing

c++ - constexpr:浮点表示错误?