为什么这个嵌套的lambda不被认为是constexpr?

时间:2017-04-27 18:38:54

标签: c++ lambda metaprogramming c++17 boost-hana

我试图使用嵌套的constexpr lambdas创建一个curried接口,但编译器并不认为它是一个常量表达式。

namespace hana = boost::hana;
using namespace hana::literals;

struct C1 {};

template < typename T,
           std::size_t size >
struct Array {};

constexpr auto array_ = [] (auto size) {
      return [=] (auto type) {
        return hana::type_c<Array<typename decltype(type)::type, size()>>;
      };
    };

int main() {

  constexpr auto c1 = hana::type_c<C1>;
  constexpr auto test = hana::type_c<Array<typename decltype(c1)::type, hana::size_c<100>()>>;
  constexpr auto test2 = array_(hana::size_c<100>)(c1);
}

我之前发布了一个问题,因为我发现了一个不同的最小例子,但它还不够。

错误:

test2.cpp: In instantiation of ‘<lambda(auto:1)>::<lambda(auto:2)> [with auto:2 = boost::hana::type_impl<C1>::_; auto:1 = boost::hana::integral_constant<long unsigned int, 100>]’:
test2.cpp:31:54:   required from here
test2.cpp:20:16: error: ‘__closure’ is not a constant expression
         return hana::type_c<Array<typename decltype(type)::type, size()>>;
                ^~~~
test2.cpp:20:16: note: in template argument for type ‘long unsigned int’ 
test2.cpp: In function ‘int main()’:
test2.cpp:31:18: error: ‘constexpr const void test2’ has incomplete type
   constexpr auto test2 = array_(hana::size_c<100>)(c1);

__closure is not a constant expression:如果有人能解释我这个错误会有很大帮助。我之前遇到过这个错误,但不记得原因。

2 个答案:

答案 0 :(得分:6)

我将您的测试用例缩减为:

#include <type_traits>

constexpr auto f = [](auto size) {
  return [=](){
    constexpr auto s = size();
    return 1;
  };
};

static_assert(f(std::integral_constant<int, 100>{})(), "");

int main() { }

正如上面的评论中所说,这是因为size不是函数体内的常量表达式。这不是Hana特有的。作为解决方法,您可以使用

constexpr auto f = [](auto size) {
  return [=](){
    constexpr auto s = decltype(size)::value;
    return 1;
  };
};

或类似的东西。

答案 1 :(得分:5)

问题是你试图在模板非类型参数中使用lambda捕获的变量之一。

  return hana::type_c<Array<typename decltype(type)::type, size()>>;
//                                                         ^~~~

模板非类型参数必须是常量表达式。在lambda中,你不能在常量表达式中使用捕获的变量。 lambda是constexpr是否无关紧要。

但是你可以在常量表达式中使用普通变量,即使它们不是constexpr变量。例如,这是合法的:

std::integral_constant<int, 100> i; // i is not constexpr
std::array<int, i()> a; // using i in a constant expression

那么为什么我们不能在常量表达式中使用捕获的变量呢?我不知道这条规则的动机,但这里有标准:

  

[expr.const]

     

(¶2)条件表达式核心常量表达式,除非......   (¶2.11)在 lambda-expression 中,对this的引用或在 lambda-expression 之外定义的自动存储持续时间的变量,其中引用这将是一种奇怪的用途。

CWG1613可能会有一些线索。

如果我们要将内部lambda重写为命名类,我们会有一个不同但相关的问题:

template <typename T>
struct Closure {
  T size;
  constexpr Closure(T size_) : size(size_) {}

  template <typename U>
  constexpr auto operator()(U type) const {
    return hana::type_c<Array<typename decltype(type)::type, size()>>;
  }
};
constexpr auto array_ = [] (auto size) {
  return Closure { size };
};

现在错误是在模板非类型参数中隐式使用this指针。

  return hana::type_c<Array<typename decltype(type)::type, size()>>;
//                                                         ^~~~~

我将Closure::operator()()声明为constexpr函数以保持一致性,但这并不重要。 this指针禁止在常量表达式中使用([expr.const]¶2.1)。声明constexpr的函数没有特别的规定来放宽可能出现在其中的常量表达式的规则。

现在原始错误更有意义,因为捕获的变量被转换为lambda闭包类型的数据成员,因此使用捕获的变量有点像通过lambda自己的“this指针”进行间接读取

这是一种解决方法,它引入了对代码的最少更改:

constexpr auto array_ = [] (auto size) {
  return [=] (auto type) {
    const auto size_ = size;
    return hana::type_c<Array<typename decltype(type)::type, size_()>>;
  };
};

现在我们使用常量表达式之外的捕获变量来初始化一个普通变量,然后我们可以在模板非类型参数中使用它。

此答案已被编辑几次,因此以下评论可能会参考之前的修订版。

相关问题