23

What's the maximum length of a source line all compilers are required to accept? Did it change in C++11? If so, what was the old value?

I'm asking this question because I'm doing some heavy preprocessor voodoo (unfortunately, templates won't cut it), and doing so has a tendency to make the lines big very quickly. I want to stay on the safe side, so I won't have to worry about the possibility of compiler X on platform Y rejecting my code because of too long lines.

  • I would rather check whether there is a way the avoid preprocessor voodoo. That is more likely than to depend on an implementation detail – stefan bachert May 09 '12 at 16:03
  • I wouldn't think it would be hard to make a C++ compiler accept infinite line lengths, but then I've never written one. – Mark Ransom May 09 '12 at 16:03
  • @stefanbachert: I can't avoid it, that's the problem. Also, the point of the question is to know where the implementation-defined land begins, so that I can avoid wandering into it. –  May 09 '12 at 16:05
  • 1
    @Fanael, it might be worth explaining if you haven't asked around already. – chris May 09 '12 at 16:05
  • 1
    @MarkRansom: sure, but there's the standard, and there are implementations. The question is about the former. –  May 09 '12 at 16:06
  • Since I had to look this up, +1. – John Dibling May 09 '12 at 16:07
  • 2
    @Mark Ransom I think it would be impossible to make a C++ compiler accept infinite line lenghths, but I will never know for sure b/c my puny computer only stores finite source files. – emory May 09 '12 at 20:12

1 Answers1

32

C++2003, Annex B, (informative) Implementation quantities (sorry, don't have C++2011 handy)

2) The limits may constrain quantities that include those described below or others. The bracketed number following each quantity is recommended as the minimum for that quantity. However, these quantities are only guidelines and do not determine compliance.

  • Characters in one logical source line [65 536].

You didn't ask about these, but they might be useful, also:

  • Nesting levels of parenthesized expressions within a full expression [256].
  • Macro identifiers simultaneously defined in one translation unit [65 536].
  • Arguments in one macro invocation [256].
  • Number of characters in an internal identifier or macro name [1 024].
  • Macro identifiers simultaneously defined in one translation unit [65 536].
  • Parameters in one macro definition [256].


Postscript: It is worth noting what "one logical source line" is. A logical source line is what you have after:
  • Physical source file characters are mapped to the basic source character set
  • Trigraph sequences (2.3) are replaced by corresponding single-character internal representations
  • Each instance of a new-line character and an immediately preceding backslash character is deleted

The logical source line is what you have before:

  • The source file is decomposed into preprocessing tokens
  • Preprocessing directives are executed and macro invocations are expanded.

[quotes from C++ 2003, 2.1 Phases of Translation]

So, if the OP's concern is that the macros expand to beyond a reasonable line length, my answer is irrelevant. If the OP's concern is that his source code (after dealing with \, \n) might be too long, my answer stands.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • 9
    Just to confirm, this is same in C++11. – Alok Save May 09 '12 at 16:05
  • 4
    +1: It is the samne in C++11: "— Characters in one logical source line [65 536]." Annex B.2 – John Dibling May 09 '12 at 16:06
  • Just to say it: A compiler is allowed to reject (or do anything else) a file containing the usual "hello world" code because of too long lines. That's undefined behavior then. I wonder why you have no clear statement in your answer about the consequences of violating the "long lines" limit... – Johannes Schaub - litb May 12 '12 at 09:34
  • @JohannesSchaub-litb: Thanks for your reply. I had involved myself in a C++ conversation today, a conversation that eventually degenerated into mockery of Linus Torvalds (who, as you know, does not care for C++). To involve you was a mistake. Sorry. If you want to know, though, I was referring to when you said that you disliked unit testing. – thb May 27 '12 at 19:32