Trigraphs in a comment, converted in c++11, ignored in c++17












8















Consider the following piece of code (notice the comment):



#include <iostream>

int main()
{
int x = 1; // <-- Why??/
x += 1;
std::cout << x << std::endl;
}


To compile this program, I am using the GNU C++ Compiler g++:



$ g++ --version // g++ (Ubuntu 6.5.0-1ubuntu1~16.04) 6.5.0 20181026


Now, when compiling this for C++11 and C++17, I get different results (and warnings).



For C++11, g++ -std=c++11 trigraph.cpp -Wall:



trigraph.cpp:5:26: warning: trigraph ??/ converted to  [-Wtrigraphs]
int x = 1; // <-- Why??/

trigraph.cpp:5:16: warning: multi-line comment [-Wcomment]
int x = 1; // <-- Why??/
^
$ ./a.out
1


For C++17, g++ -std=c++17 trigraph.cpp -Wall:



trigraph.cpp:5:26: warning: trigraph ??/ ignored, use -trigraphs to enable [-Wtrigraphs]
int x = 1; // <-- Why??/

$ ./a.out
2


After reading a bit about trigraphs, I understand that they were removed in C++17, thus ignored by the compiler as shown in the example above. However, in the case of C++11, even when it's in a comment it was converted!



Now, I can see how that would affect the code if the trigraph was in a character string for instance. But, in this example, shouldn't it be ignored since it's in a comment?



After removing the trailing forward slash ("/") from the comment, all warnings disappeared. My question is what did exactly happen here? Why the output is different?










share|improve this question


















  • 1





    Beside of knowing that something like trigraphs exists, I had wondered that they are considered even in comments. Hence, I quickly re-called Phases of translation: Phase 1: 3) Trigraph sequences are replaced by corresponding single-character representations. (until C++17), Phase 2: 1) Whenever backslash appears at the end of a line (immediately followed by the newline character), both backslash and newline are deleted, Phase 3: 3) Each comment is replaced by one space character. - This // <-- Why??/ is really underhanded. ;-)

    – Scheff
    Nov 15 '18 at 9:56













  • @Scheff thank you for the link, and also for summarizing its content to fit the question. Indeed // <-- Why??/ was tricky!

    – omar
    Nov 15 '18 at 10:09
















8















Consider the following piece of code (notice the comment):



#include <iostream>

int main()
{
int x = 1; // <-- Why??/
x += 1;
std::cout << x << std::endl;
}


To compile this program, I am using the GNU C++ Compiler g++:



$ g++ --version // g++ (Ubuntu 6.5.0-1ubuntu1~16.04) 6.5.0 20181026


Now, when compiling this for C++11 and C++17, I get different results (and warnings).



For C++11, g++ -std=c++11 trigraph.cpp -Wall:



trigraph.cpp:5:26: warning: trigraph ??/ converted to  [-Wtrigraphs]
int x = 1; // <-- Why??/

trigraph.cpp:5:16: warning: multi-line comment [-Wcomment]
int x = 1; // <-- Why??/
^
$ ./a.out
1


For C++17, g++ -std=c++17 trigraph.cpp -Wall:



trigraph.cpp:5:26: warning: trigraph ??/ ignored, use -trigraphs to enable [-Wtrigraphs]
int x = 1; // <-- Why??/

$ ./a.out
2


After reading a bit about trigraphs, I understand that they were removed in C++17, thus ignored by the compiler as shown in the example above. However, in the case of C++11, even when it's in a comment it was converted!



Now, I can see how that would affect the code if the trigraph was in a character string for instance. But, in this example, shouldn't it be ignored since it's in a comment?



After removing the trailing forward slash ("/") from the comment, all warnings disappeared. My question is what did exactly happen here? Why the output is different?










share|improve this question


















  • 1





    Beside of knowing that something like trigraphs exists, I had wondered that they are considered even in comments. Hence, I quickly re-called Phases of translation: Phase 1: 3) Trigraph sequences are replaced by corresponding single-character representations. (until C++17), Phase 2: 1) Whenever backslash appears at the end of a line (immediately followed by the newline character), both backslash and newline are deleted, Phase 3: 3) Each comment is replaced by one space character. - This // <-- Why??/ is really underhanded. ;-)

    – Scheff
    Nov 15 '18 at 9:56













  • @Scheff thank you for the link, and also for summarizing its content to fit the question. Indeed // <-- Why??/ was tricky!

    – omar
    Nov 15 '18 at 10:09














8












8








8


2






Consider the following piece of code (notice the comment):



#include <iostream>

int main()
{
int x = 1; // <-- Why??/
x += 1;
std::cout << x << std::endl;
}


To compile this program, I am using the GNU C++ Compiler g++:



$ g++ --version // g++ (Ubuntu 6.5.0-1ubuntu1~16.04) 6.5.0 20181026


Now, when compiling this for C++11 and C++17, I get different results (and warnings).



For C++11, g++ -std=c++11 trigraph.cpp -Wall:



trigraph.cpp:5:26: warning: trigraph ??/ converted to  [-Wtrigraphs]
int x = 1; // <-- Why??/

trigraph.cpp:5:16: warning: multi-line comment [-Wcomment]
int x = 1; // <-- Why??/
^
$ ./a.out
1


For C++17, g++ -std=c++17 trigraph.cpp -Wall:



trigraph.cpp:5:26: warning: trigraph ??/ ignored, use -trigraphs to enable [-Wtrigraphs]
int x = 1; // <-- Why??/

$ ./a.out
2


After reading a bit about trigraphs, I understand that they were removed in C++17, thus ignored by the compiler as shown in the example above. However, in the case of C++11, even when it's in a comment it was converted!



Now, I can see how that would affect the code if the trigraph was in a character string for instance. But, in this example, shouldn't it be ignored since it's in a comment?



After removing the trailing forward slash ("/") from the comment, all warnings disappeared. My question is what did exactly happen here? Why the output is different?










share|improve this question














Consider the following piece of code (notice the comment):



#include <iostream>

int main()
{
int x = 1; // <-- Why??/
x += 1;
std::cout << x << std::endl;
}


To compile this program, I am using the GNU C++ Compiler g++:



$ g++ --version // g++ (Ubuntu 6.5.0-1ubuntu1~16.04) 6.5.0 20181026


Now, when compiling this for C++11 and C++17, I get different results (and warnings).



For C++11, g++ -std=c++11 trigraph.cpp -Wall:



trigraph.cpp:5:26: warning: trigraph ??/ converted to  [-Wtrigraphs]
int x = 1; // <-- Why??/

trigraph.cpp:5:16: warning: multi-line comment [-Wcomment]
int x = 1; // <-- Why??/
^
$ ./a.out
1


For C++17, g++ -std=c++17 trigraph.cpp -Wall:



trigraph.cpp:5:26: warning: trigraph ??/ ignored, use -trigraphs to enable [-Wtrigraphs]
int x = 1; // <-- Why??/

$ ./a.out
2


After reading a bit about trigraphs, I understand that they were removed in C++17, thus ignored by the compiler as shown in the example above. However, in the case of C++11, even when it's in a comment it was converted!



Now, I can see how that would affect the code if the trigraph was in a character string for instance. But, in this example, shouldn't it be ignored since it's in a comment?



After removing the trailing forward slash ("/") from the comment, all warnings disappeared. My question is what did exactly happen here? Why the output is different?







c++ c++11 c++17






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 9:00









omaromar

30419




30419








  • 1





    Beside of knowing that something like trigraphs exists, I had wondered that they are considered even in comments. Hence, I quickly re-called Phases of translation: Phase 1: 3) Trigraph sequences are replaced by corresponding single-character representations. (until C++17), Phase 2: 1) Whenever backslash appears at the end of a line (immediately followed by the newline character), both backslash and newline are deleted, Phase 3: 3) Each comment is replaced by one space character. - This // <-- Why??/ is really underhanded. ;-)

    – Scheff
    Nov 15 '18 at 9:56













  • @Scheff thank you for the link, and also for summarizing its content to fit the question. Indeed // <-- Why??/ was tricky!

    – omar
    Nov 15 '18 at 10:09














  • 1





    Beside of knowing that something like trigraphs exists, I had wondered that they are considered even in comments. Hence, I quickly re-called Phases of translation: Phase 1: 3) Trigraph sequences are replaced by corresponding single-character representations. (until C++17), Phase 2: 1) Whenever backslash appears at the end of a line (immediately followed by the newline character), both backslash and newline are deleted, Phase 3: 3) Each comment is replaced by one space character. - This // <-- Why??/ is really underhanded. ;-)

    – Scheff
    Nov 15 '18 at 9:56













  • @Scheff thank you for the link, and also for summarizing its content to fit the question. Indeed // <-- Why??/ was tricky!

    – omar
    Nov 15 '18 at 10:09








1




1





Beside of knowing that something like trigraphs exists, I had wondered that they are considered even in comments. Hence, I quickly re-called Phases of translation: Phase 1: 3) Trigraph sequences are replaced by corresponding single-character representations. (until C++17), Phase 2: 1) Whenever backslash appears at the end of a line (immediately followed by the newline character), both backslash and newline are deleted, Phase 3: 3) Each comment is replaced by one space character. - This // <-- Why??/ is really underhanded. ;-)

– Scheff
Nov 15 '18 at 9:56







Beside of knowing that something like trigraphs exists, I had wondered that they are considered even in comments. Hence, I quickly re-called Phases of translation: Phase 1: 3) Trigraph sequences are replaced by corresponding single-character representations. (until C++17), Phase 2: 1) Whenever backslash appears at the end of a line (immediately followed by the newline character), both backslash and newline are deleted, Phase 3: 3) Each comment is replaced by one space character. - This // <-- Why??/ is really underhanded. ;-)

– Scheff
Nov 15 '18 at 9:56















@Scheff thank you for the link, and also for summarizing its content to fit the question. Indeed // <-- Why??/ was tricky!

– omar
Nov 15 '18 at 10:09





@Scheff thank you for the link, and also for summarizing its content to fit the question. Indeed // <-- Why??/ was tricky!

– omar
Nov 15 '18 at 10:09












3 Answers
3






active

oldest

votes


















5














Trigraphs are are very old way to insert certain characters into code, which were possibly not available on all keybords. For full list, see cppreference.



In your example, you accidentally created one of the trigraphs ??/, which is translated into . Trailing has a special meaning - it tells the compiler to ignore line break and treat next line as part of the current line.



Your code would be translated like this:



int x = 1; // <-- Why??/
x += 1;

int x = 1; // <-- Why
x += 1;

int x = 1; // <-- Why x += 1;


This is what the warnings actually mean.
Trigraph was interpreted and changed into , and it created a multi line comment, even though you used //.



Now, trigraphs became depracated in C++11 and were removed from standard in C++17.
This means, when compiling in C++11 your trigraph was translated, but in C++17 it was ignored (and compiler sent you a note that you can still enable them).






share|improve this answer
























  • So the x += 1; statement got stripped out during preprocessing, makes sense. Thanks!

    – omar
    Nov 15 '18 at 9:23



















5














The trigraph ??/ gets converted by the compiler to before actual compilation happens (i.e. before comments are removed).



So these lines



int x = 1; // <-- Why??/
x += 1;


get converted to



int x = 1; // <-- Why
x += 1;


A backslash at the end of a line appends the next line to it. So it becomes



int x = 1; // <-- Why    x += 1;


Which moves the statement x+=1; into the comment and thus it is not compiled.



When you remove the trailing /, it is not a trigraph anymore (sincce it's now only ??) and nothing special happens.






share|improve this answer































    3














    If ??/ is converted to then ??// would be converted to /.



    Or // also starts a comment, so the application rule for trigraphs comes first, and only after that does the compiler check whether it's a comment or not.






    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%2f53315710%2ftrigraphs-in-a-comment-converted-in-c11-ignored-in-c17%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5














      Trigraphs are are very old way to insert certain characters into code, which were possibly not available on all keybords. For full list, see cppreference.



      In your example, you accidentally created one of the trigraphs ??/, which is translated into . Trailing has a special meaning - it tells the compiler to ignore line break and treat next line as part of the current line.



      Your code would be translated like this:



      int x = 1; // <-- Why??/
      x += 1;

      int x = 1; // <-- Why
      x += 1;

      int x = 1; // <-- Why x += 1;


      This is what the warnings actually mean.
      Trigraph was interpreted and changed into , and it created a multi line comment, even though you used //.



      Now, trigraphs became depracated in C++11 and were removed from standard in C++17.
      This means, when compiling in C++11 your trigraph was translated, but in C++17 it was ignored (and compiler sent you a note that you can still enable them).






      share|improve this answer
























      • So the x += 1; statement got stripped out during preprocessing, makes sense. Thanks!

        – omar
        Nov 15 '18 at 9:23
















      5














      Trigraphs are are very old way to insert certain characters into code, which were possibly not available on all keybords. For full list, see cppreference.



      In your example, you accidentally created one of the trigraphs ??/, which is translated into . Trailing has a special meaning - it tells the compiler to ignore line break and treat next line as part of the current line.



      Your code would be translated like this:



      int x = 1; // <-- Why??/
      x += 1;

      int x = 1; // <-- Why
      x += 1;

      int x = 1; // <-- Why x += 1;


      This is what the warnings actually mean.
      Trigraph was interpreted and changed into , and it created a multi line comment, even though you used //.



      Now, trigraphs became depracated in C++11 and were removed from standard in C++17.
      This means, when compiling in C++11 your trigraph was translated, but in C++17 it was ignored (and compiler sent you a note that you can still enable them).






      share|improve this answer
























      • So the x += 1; statement got stripped out during preprocessing, makes sense. Thanks!

        – omar
        Nov 15 '18 at 9:23














      5












      5








      5







      Trigraphs are are very old way to insert certain characters into code, which were possibly not available on all keybords. For full list, see cppreference.



      In your example, you accidentally created one of the trigraphs ??/, which is translated into . Trailing has a special meaning - it tells the compiler to ignore line break and treat next line as part of the current line.



      Your code would be translated like this:



      int x = 1; // <-- Why??/
      x += 1;

      int x = 1; // <-- Why
      x += 1;

      int x = 1; // <-- Why x += 1;


      This is what the warnings actually mean.
      Trigraph was interpreted and changed into , and it created a multi line comment, even though you used //.



      Now, trigraphs became depracated in C++11 and were removed from standard in C++17.
      This means, when compiling in C++11 your trigraph was translated, but in C++17 it was ignored (and compiler sent you a note that you can still enable them).






      share|improve this answer













      Trigraphs are are very old way to insert certain characters into code, which were possibly not available on all keybords. For full list, see cppreference.



      In your example, you accidentally created one of the trigraphs ??/, which is translated into . Trailing has a special meaning - it tells the compiler to ignore line break and treat next line as part of the current line.



      Your code would be translated like this:



      int x = 1; // <-- Why??/
      x += 1;

      int x = 1; // <-- Why
      x += 1;

      int x = 1; // <-- Why x += 1;


      This is what the warnings actually mean.
      Trigraph was interpreted and changed into , and it created a multi line comment, even though you used //.



      Now, trigraphs became depracated in C++11 and were removed from standard in C++17.
      This means, when compiling in C++11 your trigraph was translated, but in C++17 it was ignored (and compiler sent you a note that you can still enable them).







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 15 '18 at 9:11









      YksisarvinenYksisarvinen

      1,628519




      1,628519













      • So the x += 1; statement got stripped out during preprocessing, makes sense. Thanks!

        – omar
        Nov 15 '18 at 9:23



















      • So the x += 1; statement got stripped out during preprocessing, makes sense. Thanks!

        – omar
        Nov 15 '18 at 9:23

















      So the x += 1; statement got stripped out during preprocessing, makes sense. Thanks!

      – omar
      Nov 15 '18 at 9:23





      So the x += 1; statement got stripped out during preprocessing, makes sense. Thanks!

      – omar
      Nov 15 '18 at 9:23













      5














      The trigraph ??/ gets converted by the compiler to before actual compilation happens (i.e. before comments are removed).



      So these lines



      int x = 1; // <-- Why??/
      x += 1;


      get converted to



      int x = 1; // <-- Why
      x += 1;


      A backslash at the end of a line appends the next line to it. So it becomes



      int x = 1; // <-- Why    x += 1;


      Which moves the statement x+=1; into the comment and thus it is not compiled.



      When you remove the trailing /, it is not a trigraph anymore (sincce it's now only ??) and nothing special happens.






      share|improve this answer




























        5














        The trigraph ??/ gets converted by the compiler to before actual compilation happens (i.e. before comments are removed).



        So these lines



        int x = 1; // <-- Why??/
        x += 1;


        get converted to



        int x = 1; // <-- Why
        x += 1;


        A backslash at the end of a line appends the next line to it. So it becomes



        int x = 1; // <-- Why    x += 1;


        Which moves the statement x+=1; into the comment and thus it is not compiled.



        When you remove the trailing /, it is not a trigraph anymore (sincce it's now only ??) and nothing special happens.






        share|improve this answer


























          5












          5








          5







          The trigraph ??/ gets converted by the compiler to before actual compilation happens (i.e. before comments are removed).



          So these lines



          int x = 1; // <-- Why??/
          x += 1;


          get converted to



          int x = 1; // <-- Why
          x += 1;


          A backslash at the end of a line appends the next line to it. So it becomes



          int x = 1; // <-- Why    x += 1;


          Which moves the statement x+=1; into the comment and thus it is not compiled.



          When you remove the trailing /, it is not a trigraph anymore (sincce it's now only ??) and nothing special happens.






          share|improve this answer













          The trigraph ??/ gets converted by the compiler to before actual compilation happens (i.e. before comments are removed).



          So these lines



          int x = 1; // <-- Why??/
          x += 1;


          get converted to



          int x = 1; // <-- Why
          x += 1;


          A backslash at the end of a line appends the next line to it. So it becomes



          int x = 1; // <-- Why    x += 1;


          Which moves the statement x+=1; into the comment and thus it is not compiled.



          When you remove the trailing /, it is not a trigraph anymore (sincce it's now only ??) and nothing special happens.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 9:07









          davedave

          613110




          613110























              3














              If ??/ is converted to then ??// would be converted to /.



              Or // also starts a comment, so the application rule for trigraphs comes first, and only after that does the compiler check whether it's a comment or not.






              share|improve this answer




























                3














                If ??/ is converted to then ??// would be converted to /.



                Or // also starts a comment, so the application rule for trigraphs comes first, and only after that does the compiler check whether it's a comment or not.






                share|improve this answer


























                  3












                  3








                  3







                  If ??/ is converted to then ??// would be converted to /.



                  Or // also starts a comment, so the application rule for trigraphs comes first, and only after that does the compiler check whether it's a comment or not.






                  share|improve this answer













                  If ??/ is converted to then ??// would be converted to /.



                  Or // also starts a comment, so the application rule for trigraphs comes first, and only after that does the compiler check whether it's a comment or not.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 9:06









                  GeoffroyGeoffroy

                  9,15033383




                  9,15033383






























                      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%2f53315710%2ftrigraphs-in-a-comment-converted-in-c11-ignored-in-c17%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

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python