Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8855911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:14:31+00:00 2026-06-14T14:14:31+00:00

GCC 4.7.2 compiles this: constexpr int i = 5; []{ std::integral_constant< int, i >();

  • 0

GCC 4.7.2 compiles this:

constexpr int i = 5;
[]{ std::integral_constant< int, i >(); }; // nonstandard: i not captured

but not this:

constexpr int i = 5;
[&i]{ std::integral_constant< int, i >(); }; // GCC says i not constexpr

The latter example appears correct to me, according to C++11 §5.1.2/15:

An entity is captured by reference if it is implicitly or explicitly captured but not captured by copy. It is unspecified whether additional unnamed non-static data members are declared in the closure type for entities captured by reference.

It seems the captured object i inside the lambda refers to the variable in the enclosing scope, which is constexpr, not merely a const reference.

The standard explicitly says that the use of a by-value capture is transformed into a use of the corresponding member of the lambda object. And I think that 5.1.2 hints that my interpretation is correct.

Is there anything that explicitly says that whether a capture by reference refers to the object in the enclosing scope or a reference?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:14:33+00:00Added an answer on June 14, 2026 at 2:14 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code compiles fine with GCC and Clang but not with MSVC 2010: #include
This code compiles just fine on gcc, but when using llvm (llvm-gcc), it says
For example, gcc compiles this ok... char s[7] = abc; But it gives the
This compiles fine in MSVC but gcc complains. The code on ideone . I
This compiles on GCC 4.6 but doesn't with VS2010 sp1: Is it my fault
Consider this example: #include <algorithm> #include <iostream> int main() { std::string str = abcde4fghijk4l5mnopqrs6t8uvwxyz;
I wrote this code that compiles on Solaris gcc it works fine too for
This piece of code compiles and runs as expected on GCC 3.x and 4.x:
I was trying to execute this code through gcc compiler: #include <stdio.h> int main()
In gcc I can do compile-time warnings like this: #if !defined(_SOME_FEATURE_) #warning _SOME_FEATURE_ not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.