Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 464
JVApen Avatar asked Aug 18 '19 20:08

JVApen


People also ask

What is a source file in programming?

(1) A file that contains program instructions. See source code. (2) A file that contains original or essential data that is the starting point for a system of publishing or other processing.

How do you send source code?

If you just need the source code, you could copy the whole folder. If you're looking for the code, look for the C# (. cs) files within the folder, and open them with notepad++ or something later on. If you want the project, just copy the whole project folder over.

What is expanded source code?

The expanded Cobol code contains a comment that is inserted before a copy expansion, full path of the Copy file used with the source line number for the Copy/Include statement, and a comment inserted at the end of the expansion with the same copy file information (path and line number) matching the start comment.


2 Answers

No, source code doesn't have to come from a file (nor go to a file).

You can compile (and link) 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 this state of affairs. 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 std::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).

@HBv6 notes that 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>.

Source code can even come from the Internet.

@Morwenn notes that this code:

#include <https://raw.githubusercontent.com/Morwenn/poplar-heap/master/poplar.h>  // Type your code here, or load an example. void poplar_sort(int* data, size_t size) {     poplar::make_heap(data, data + size);     poplar::sort_heap(data, data + size); } 

works on GodBolt (but won't work on your machine - no popular compiler supports this.)

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 source file". But then, where do the includes come from? One would assume they come from named files on the filesystem... but that too is not mandated.

At any rate, std::source_location does not seem to change this wording in C++20 or to affect its interpretation.

like image 106
einpoklum Avatar answered Sep 16 '22 14:09

einpoklum


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

__FILE__ 

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

The definition is the same for source_location::file_name.

As such, there has not been a change in regard to support for file system-less 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.

like image 43
eerorika Avatar answered Sep 18 '22 14:09

eerorika