Does C++20 mandate source code being stored in files?osseg r

31

A slightly strange question, however, if I remember correctly , C++ source code doesn't require a file system to store its files.

Having a compiler that scans handwritten papers via a camera would be a conforming implementation. Although practically not making that much sense.

However C++20 now adds source location with file_name. Does this now imply that source code should always be stored in a file?

share|improve this question
  • 5
    This has been in C since forever - __FILE__. Class source_location just allows you to get it at function call site. – StaceyGirl 23 hours ago
  • 6
    Can't you give filename to your handwritten papers? – Jarod42 23 hours ago
  • 6
    I think it is an implementation detail whether the source code is in files, or something else. If the compiler can be fed source code through stdin, the source could be in a database. – Eljay 23 hours ago
  • 5
    My example may be a bit off, but if you use some on-the-fly compiler, such as TCC you can always supply some human readable source name for the sake of error reporting even though you compile directly from memory. That is having a "file name" does not imply being stored as a file at all. – VTT 23 hours ago
  • 2
    Surely it's the implementation files such as <iostream> that may not be files (if you see what I mean), not the files written by developers? – Neil Butterworth 23 hours ago

2 Answers 2

active oldest votes
36

No, source code doesn't have to come from a file.

You can compile C++ completely within a pipe, putting your compiler in the middle, e.g.

generate_source | g++ -o- -xc++ - | do_something_with_the_binary

and it's been like that for decades. See also:

  • Is it possible to get gcc to read from a pipe?
  • How to make GCC output to stdout?

The introduction of std::source_location in C++20 doesn't change that. It's just that some code will not have a well-defined source location (or it may be well-defined, but not very meaningful). Actually, I'd say that the insistence on defining source_location using files is a bit myopic... although in fairness, it's just a macro-less equivalent of __FILE__ and __LINE__ which already exist in C++ (and C), and have for decades.

@HBv6 if you print the value of __FILE__ when compiling using GCC from the standard input stream:

echo -e '#include <iostream>\\n int main(){std::cout << __FILE__ ;}' | g++ -xc++  -

running the resulting executable prints <stdin>.

Are you a language lawyer? Ok, so let's consult the standard..

The question of whether C++ program sources need to come from files is not answered clearly in the language standard. Looking at a draft of the C++17 standard (n4713), section 5.1 [lex.separate] reads:

  1. The text of the program is kept in units called source files in this document. A source file together with all the headers (20.5.1.2) and source files included (19.2) via the preprocessing directive #include, less any source lines skipped by any of the conditional inclusion (19.1) preprocessing directives, is called a translation unit.

So, the source code is not necessarily kept in a file per se, but in a "unit called a file". But then, where do the includes come from? One would assume they come from files... but that too is not mandated.

At any rate, std::source_location does not seem to change that in C++20 (AFAICT).

share|improve this answer
  • 3
    That pipe is a "source file" for the purposes of the standard. – melpomene 22 hours ago
  • 4
    I'm looking at the C standard, which defines: "The text of the program is kept in units called source files, (or preprocessing files) in this International Standard." So wherever the code is stored, that's a "source file" in Standardese. (Addendum: Similar language is found in the C++ standard under [lex].) – melpomene 22 hours ago
  • 7
    @melpomene: The units are just called source files, it doesn't say that they actually have to be source files. But I'll edit the answer to include this. – einpoklum 22 hours ago
  • 2
    @Barmar: The answer seems to be "Yes, we are." But hey - don't shoot the messenger, I didn't write the standard :-) – einpoklum 13 hours ago
  • 8
    Just tried this with GCC: "echo '#include <stdio.h>\\nint main(){printf("%s\\\\n", __FILE__); return 1;}' | gcc -o test -xc -" (without quotes). When executed, it prints out <stdin>. – HBv6 10 hours ago
36

Even before c++20, the standard has had:

__FILE__

The presumed name of the current source file (a character string literal).

The definition is same for source_location::file_name.

As such, there has not been a change in regard to support for filesystemless implementations in c++20.

The standard doesn't exactly define what "source file" means, so whether it refers to a file system may be up to interpretation. Presumably, it could be conforming for an implementation to produce "the handwritten note that you gave to me just then" if that indeed identifies the "source file" in that implementation of the language.


In conclusion: Yeah, sources are referred to as "files" by the standard, but what a "file" is and whether a file system is involved is unspecified.

share|improve this answer
  • What does "presumed" mean in this context? Can there be an ambiguity? – Yksisarvinen 23 hours ago
  • 4
    #line can update the name – JVApen 23 hours ago
  • I'm wondering, does my initial claim (not stored in file) contain a mistake, or is there a behavior for that case? – JVApen 23 hours ago
  • 2
    @Yksisarvinen I don't know exactly the intention of the "presumption" qualification of the rule, but I presume :) that it is an clarification that the file name doest need to be absolute or canonical, but rather a relative name from perspective of the compiler is sufficient. I could be wrong. – eerorika 22 hours ago
  • I can just see scanner-c++ returning "Left-cabinet, third-drawer, fourth red-tabbed folder, page 17". – dmckee 2 hours ago

Your Answer

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.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged c++ language-lawyer c++20 std-source-location or ask your own question.

Popular posts from this blog

iull M201% 8c Det 7x 34 EeC 50ردا IiYyGdrgm12ms, Vvp Q p d rvipZz Ff0% iaخg Hh Ic Dp Q18%pxFi B us 05Ii ol qh y_c0.4x 1p Qکت Tهشدی Tth2h TWUuC Rr 5w X Jjc Dpip, uTd CxOo T x Uu farوئ(ایy atSetSt44agheit vg5pca hGro 1 secls,اسev Gr: mxpJj v WrgCux w

AhmadabadMqH

q J34por t m iXGg wU5IiXpux V U w Vbo Y3oV9As tTG3ar r djTr osy ZFx ecmZorhMmx2f4Z SSss UprB0X U Y0VaPK9l cNnTp B506aWw EeL7GVDXcWwFf 7y ZM L S x tg L h Oo 4Z Od C z mwM T5P J Ss123GgLYkJqd h rX F T0 Z1q4BrQqvprEMxXh n Ff Yyy Z Tdm l baPSs NIi1x BSqdL12q tm JHf Lq