result of evaluating block statement in c++












1















I have the simplest c++ statement declaring a variable "a":



int a = ({ int b = 10; b; });


As recent gcc and clang says, it's a valid statement which really declares variable a having value 10.



The question is: what is this? I know about various types of expressions. I know about various types of statements. But I can't find in a c++ 14 standard any mentioning that "block statement as an expression returns latest inner evaluated expression" or something like this.



Could somebody please point me exact lines of a standard saying that is code line is fully valid?










share|improve this question




















  • 2





    This is not C++. If you want to be a language lawyer, you need to be -pedantic.

    – n.m.
    Nov 24 '18 at 11:11













  • I added the language-lawyer tag because of the mention of finding the relevant C++ standard paragraph. I should have checked that it actually compiled first. My bad. Removed it.

    – Matthieu Brucher
    Nov 24 '18 at 11:13













  • So whether I understand you right that a fact of successful compilation of this line with g++ 5.4 and clang++ 6.0 ("clang++-6.8 -std=c++14 -Wall -o 1.out 1.cpp") is an extension of both compilers? Upd: checked with -pedantic. Yes, a compiler says that is an extension.

    – Vyacheslav Grigoryev
    Nov 24 '18 at 11:14













  • A standard conforming way to do something like this would be to use a directly invoked lambda - like int a = { int b = 10; return b; }(); .

    – Jesper Juhl
    Nov 24 '18 at 11:34
















1















I have the simplest c++ statement declaring a variable "a":



int a = ({ int b = 10; b; });


As recent gcc and clang says, it's a valid statement which really declares variable a having value 10.



The question is: what is this? I know about various types of expressions. I know about various types of statements. But I can't find in a c++ 14 standard any mentioning that "block statement as an expression returns latest inner evaluated expression" or something like this.



Could somebody please point me exact lines of a standard saying that is code line is fully valid?










share|improve this question




















  • 2





    This is not C++. If you want to be a language lawyer, you need to be -pedantic.

    – n.m.
    Nov 24 '18 at 11:11













  • I added the language-lawyer tag because of the mention of finding the relevant C++ standard paragraph. I should have checked that it actually compiled first. My bad. Removed it.

    – Matthieu Brucher
    Nov 24 '18 at 11:13













  • So whether I understand you right that a fact of successful compilation of this line with g++ 5.4 and clang++ 6.0 ("clang++-6.8 -std=c++14 -Wall -o 1.out 1.cpp") is an extension of both compilers? Upd: checked with -pedantic. Yes, a compiler says that is an extension.

    – Vyacheslav Grigoryev
    Nov 24 '18 at 11:14













  • A standard conforming way to do something like this would be to use a directly invoked lambda - like int a = { int b = 10; return b; }(); .

    – Jesper Juhl
    Nov 24 '18 at 11:34














1












1








1








I have the simplest c++ statement declaring a variable "a":



int a = ({ int b = 10; b; });


As recent gcc and clang says, it's a valid statement which really declares variable a having value 10.



The question is: what is this? I know about various types of expressions. I know about various types of statements. But I can't find in a c++ 14 standard any mentioning that "block statement as an expression returns latest inner evaluated expression" or something like this.



Could somebody please point me exact lines of a standard saying that is code line is fully valid?










share|improve this question
















I have the simplest c++ statement declaring a variable "a":



int a = ({ int b = 10; b; });


As recent gcc and clang says, it's a valid statement which really declares variable a having value 10.



The question is: what is this? I know about various types of expressions. I know about various types of statements. But I can't find in a c++ 14 standard any mentioning that "block statement as an expression returns latest inner evaluated expression" or something like this.



Could somebody please point me exact lines of a standard saying that is code line is fully valid?







c++ c++14






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 11:14









Matthieu Brucher

13.5k32140




13.5k32140










asked Nov 24 '18 at 11:06









Vyacheslav GrigoryevVyacheslav Grigoryev

1014




1014








  • 2





    This is not C++. If you want to be a language lawyer, you need to be -pedantic.

    – n.m.
    Nov 24 '18 at 11:11













  • I added the language-lawyer tag because of the mention of finding the relevant C++ standard paragraph. I should have checked that it actually compiled first. My bad. Removed it.

    – Matthieu Brucher
    Nov 24 '18 at 11:13













  • So whether I understand you right that a fact of successful compilation of this line with g++ 5.4 and clang++ 6.0 ("clang++-6.8 -std=c++14 -Wall -o 1.out 1.cpp") is an extension of both compilers? Upd: checked with -pedantic. Yes, a compiler says that is an extension.

    – Vyacheslav Grigoryev
    Nov 24 '18 at 11:14













  • A standard conforming way to do something like this would be to use a directly invoked lambda - like int a = { int b = 10; return b; }(); .

    – Jesper Juhl
    Nov 24 '18 at 11:34














  • 2





    This is not C++. If you want to be a language lawyer, you need to be -pedantic.

    – n.m.
    Nov 24 '18 at 11:11













  • I added the language-lawyer tag because of the mention of finding the relevant C++ standard paragraph. I should have checked that it actually compiled first. My bad. Removed it.

    – Matthieu Brucher
    Nov 24 '18 at 11:13













  • So whether I understand you right that a fact of successful compilation of this line with g++ 5.4 and clang++ 6.0 ("clang++-6.8 -std=c++14 -Wall -o 1.out 1.cpp") is an extension of both compilers? Upd: checked with -pedantic. Yes, a compiler says that is an extension.

    – Vyacheslav Grigoryev
    Nov 24 '18 at 11:14













  • A standard conforming way to do something like this would be to use a directly invoked lambda - like int a = { int b = 10; return b; }(); .

    – Jesper Juhl
    Nov 24 '18 at 11:34








2




2





This is not C++. If you want to be a language lawyer, you need to be -pedantic.

– n.m.
Nov 24 '18 at 11:11







This is not C++. If you want to be a language lawyer, you need to be -pedantic.

– n.m.
Nov 24 '18 at 11:11















I added the language-lawyer tag because of the mention of finding the relevant C++ standard paragraph. I should have checked that it actually compiled first. My bad. Removed it.

– Matthieu Brucher
Nov 24 '18 at 11:13







I added the language-lawyer tag because of the mention of finding the relevant C++ standard paragraph. I should have checked that it actually compiled first. My bad. Removed it.

– Matthieu Brucher
Nov 24 '18 at 11:13















So whether I understand you right that a fact of successful compilation of this line with g++ 5.4 and clang++ 6.0 ("clang++-6.8 -std=c++14 -Wall -o 1.out 1.cpp") is an extension of both compilers? Upd: checked with -pedantic. Yes, a compiler says that is an extension.

– Vyacheslav Grigoryev
Nov 24 '18 at 11:14







So whether I understand you right that a fact of successful compilation of this line with g++ 5.4 and clang++ 6.0 ("clang++-6.8 -std=c++14 -Wall -o 1.out 1.cpp") is an extension of both compilers? Upd: checked with -pedantic. Yes, a compiler says that is an extension.

– Vyacheslav Grigoryev
Nov 24 '18 at 11:14















A standard conforming way to do something like this would be to use a directly invoked lambda - like int a = { int b = 10; return b; }(); .

– Jesper Juhl
Nov 24 '18 at 11:34





A standard conforming way to do something like this would be to use a directly invoked lambda - like int a = { int b = 10; return b; }(); .

– Jesper Juhl
Nov 24 '18 at 11:34












1 Answer
1






active

oldest

votes


















2















The question is: what is this?




This is a GNU extension to ISO standard C, an extension that is available also for C++, but likewise is not part of ISO C++.



Citing the GCC Manual: Chapter 6 - Extensions to the C Language Family:




...



These extensions are available in C and Objective-C. Most of them are also available in C++. ...




  • Statement Exprs: Putting statements and declarations inside expressions.




Where the latter is explained in detail in GCC Manual: Section 6.1 - Statements and Declarations in Expressions:




A compound statement enclosed in parentheses may appear as an expression in GNU C. This allows you to use loops, switches, and local variables within an expression.



Recall that a compound statement is a sequence of statements surrounded by braces; in this construct, parentheses go around the braces. For example:



({ int y = foo (); int z;
if (y > 0) z = y;
else z = - y;
z; })


is a valid (though slightly more complex than necessary) expression for the absolute value of foo ().



...




As for Clang, the Clang Language Extensions describes that Clang aims to support many GCC extensions: [emhpasis mine]:




This document describes the language extensions provided by Clang. In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions. Please see the GCC manual for more information on these extensions.







share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457490%2fresult-of-evaluating-block-statement-in-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2















    The question is: what is this?




    This is a GNU extension to ISO standard C, an extension that is available also for C++, but likewise is not part of ISO C++.



    Citing the GCC Manual: Chapter 6 - Extensions to the C Language Family:




    ...



    These extensions are available in C and Objective-C. Most of them are also available in C++. ...




    • Statement Exprs: Putting statements and declarations inside expressions.




    Where the latter is explained in detail in GCC Manual: Section 6.1 - Statements and Declarations in Expressions:




    A compound statement enclosed in parentheses may appear as an expression in GNU C. This allows you to use loops, switches, and local variables within an expression.



    Recall that a compound statement is a sequence of statements surrounded by braces; in this construct, parentheses go around the braces. For example:



    ({ int y = foo (); int z;
    if (y > 0) z = y;
    else z = - y;
    z; })


    is a valid (though slightly more complex than necessary) expression for the absolute value of foo ().



    ...




    As for Clang, the Clang Language Extensions describes that Clang aims to support many GCC extensions: [emhpasis mine]:




    This document describes the language extensions provided by Clang. In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions. Please see the GCC manual for more information on these extensions.







    share|improve this answer






























      2















      The question is: what is this?




      This is a GNU extension to ISO standard C, an extension that is available also for C++, but likewise is not part of ISO C++.



      Citing the GCC Manual: Chapter 6 - Extensions to the C Language Family:




      ...



      These extensions are available in C and Objective-C. Most of them are also available in C++. ...




      • Statement Exprs: Putting statements and declarations inside expressions.




      Where the latter is explained in detail in GCC Manual: Section 6.1 - Statements and Declarations in Expressions:




      A compound statement enclosed in parentheses may appear as an expression in GNU C. This allows you to use loops, switches, and local variables within an expression.



      Recall that a compound statement is a sequence of statements surrounded by braces; in this construct, parentheses go around the braces. For example:



      ({ int y = foo (); int z;
      if (y > 0) z = y;
      else z = - y;
      z; })


      is a valid (though slightly more complex than necessary) expression for the absolute value of foo ().



      ...




      As for Clang, the Clang Language Extensions describes that Clang aims to support many GCC extensions: [emhpasis mine]:




      This document describes the language extensions provided by Clang. In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions. Please see the GCC manual for more information on these extensions.







      share|improve this answer




























        2












        2








        2








        The question is: what is this?




        This is a GNU extension to ISO standard C, an extension that is available also for C++, but likewise is not part of ISO C++.



        Citing the GCC Manual: Chapter 6 - Extensions to the C Language Family:




        ...



        These extensions are available in C and Objective-C. Most of them are also available in C++. ...




        • Statement Exprs: Putting statements and declarations inside expressions.




        Where the latter is explained in detail in GCC Manual: Section 6.1 - Statements and Declarations in Expressions:




        A compound statement enclosed in parentheses may appear as an expression in GNU C. This allows you to use loops, switches, and local variables within an expression.



        Recall that a compound statement is a sequence of statements surrounded by braces; in this construct, parentheses go around the braces. For example:



        ({ int y = foo (); int z;
        if (y > 0) z = y;
        else z = - y;
        z; })


        is a valid (though slightly more complex than necessary) expression for the absolute value of foo ().



        ...




        As for Clang, the Clang Language Extensions describes that Clang aims to support many GCC extensions: [emhpasis mine]:




        This document describes the language extensions provided by Clang. In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions. Please see the GCC manual for more information on these extensions.







        share|improve this answer
















        The question is: what is this?




        This is a GNU extension to ISO standard C, an extension that is available also for C++, but likewise is not part of ISO C++.



        Citing the GCC Manual: Chapter 6 - Extensions to the C Language Family:




        ...



        These extensions are available in C and Objective-C. Most of them are also available in C++. ...




        • Statement Exprs: Putting statements and declarations inside expressions.




        Where the latter is explained in detail in GCC Manual: Section 6.1 - Statements and Declarations in Expressions:




        A compound statement enclosed in parentheses may appear as an expression in GNU C. This allows you to use loops, switches, and local variables within an expression.



        Recall that a compound statement is a sequence of statements surrounded by braces; in this construct, parentheses go around the braces. For example:



        ({ int y = foo (); int z;
        if (y > 0) z = y;
        else z = - y;
        z; })


        is a valid (though slightly more complex than necessary) expression for the absolute value of foo ().



        ...




        As for Clang, the Clang Language Extensions describes that Clang aims to support many GCC extensions: [emhpasis mine]:




        This document describes the language extensions provided by Clang. In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions. Please see the GCC manual for more information on these extensions.








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 11:46

























        answered Nov 24 '18 at 11:26









        dfridfri

        35.1k45696




        35.1k45696






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457490%2fresult-of-evaluating-block-statement-in-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)