Lambda capturing constexpr object

杀马特。学长 韩版系。学妹 提交于 2019-11-28 13:16:15

The second template-argument to std::integral_constant< int, i > is for a template-parameter of non-type form, specifically of integral or enumeration type (14.3.2p1 bullet 1) and so must be a converted constant expression of type int.

In a lambda-expression, implicit capture occurs when an entity is odr-used in the compound statement (5.1.2p11); use of a converted constant expression in an explicit template instantiation is not odr-use (3.2p3), so the first example is valid.

In the second example, I think gcc is incorrect to reject it; 5.1.2p17 says in a note that:

An id-expression that is not an odr-use refers to the original entity, never to a member of the closure type.

Although the paragraph as a whole is discussing capture by copy, there's no reason not to apply this rule to capture by reference as well. It's unsurprising that the standard is unclear on this; there's really no reason to capture an entity that can be used in a converted constant expression by reference.

First, I can confirm your observation with gcc 4.6.3 and clang 3.0 on Ubuntu 12.04.

I don't have the C++11 standard (only draft), so I cannot comment on that. But look at the, from my understanding, equivalent statements

constexpr int i = 5;
const int &j = i;
std::integral_constant<int, j>();

Neither gcc, nor clang compiles this, because j is not an "integral constant".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!