C ++ 20是否要求将源代码存储在文件中? [英] Does C++20 mandate source code being stored in files?
问题描述
但是,如果我没记错的话,一个稍微奇怪的问题是C ++源代码不需要文件系统来存储其文件。
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.
但是C ++ 20现在使用 文件名
。
However C++20 now adds source location with file_name
. Does this now imply that source code should always be stored in a file?
推荐答案
不,源代码不一定要存储在文件中吗?
您可以在管道内完全编译(并链接)C ++,例如将编译器置于中间。
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?
std的引入: C ++ 20中的:source_location
不会更改此状态。只是有些代码没有明确定义的源位置(或者可能定义明确,但意义不大)。实际上,我想说的是坚持使用文件定义 std :: source_location
有点近视……尽管公平地说,它只不过是 __ FILE __
和 __ LINE __
在C ++(和C)中已经存在。
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指出,如果从标准输入流使用GCC进行编译时,如果打印 __ FILE __
的值,则:
@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++ -
运行生成的可执行文件< stdin>
。
running the resulting executable prints <stdin>
.
@Morwenn指出代码:
@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);
}
可在GodBolt上运行(但无法在您的计算机上运行-没有流行的编译器支持此功能。)
works on GodBolt (but won't work on your machine - no popular compiler supports this.)
在语言标准中,对于C ++程序源是否需要来自文件的问题并没有明确回答。查看C ++ 17标准(n4713)的草稿,第5.1节[lex.separate]读取:
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:
- 程序的文本以本文档中称为源文件的单元形式保存。通过预处理指令#include将源文件连同所有标头(20.5.1.2)和包含的源文件(19.2)一起,减去通过任何条件包含(19.1)预处理指令跳过的任何源行,称为转换单元。
因此,源代码本身不一定保存在文件中,而是保存在单元称为源文件。但是,包含项从何而来?有人会假设它们来自文件系统上的命名文件...但是也没有强制要求。
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.
无论如何, std :: source_location
似乎并没有改变C ++ 20中的措辞或影响其解释(AFAICT)。
At any rate, std::source_location
does not seem to change this wording in C++20 or to affect its interpretation (AFAICT).
这篇关于C ++ 20是否要求将源代码存储在文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!