#include 在 C++ 中的 HTTP URL

#include of a HTTP URL in C++

我在 Github ( https://github.com/Quuxplusone/coro/blob/master/examples/pythagorean_triples_generator.cpp ), and was surprised to see that it actually compiles ( https://coro.godbolt.org/z/JXTX4Y ) 上查看随机 C++ 示例。

#include <https://raw.githubusercontent.com/Quuxplusone/coro/master/include/coro/shared_generator.h>
#include <stdio.h>
#include <tuple>
#include <range/v3/view/take.hpp>

namespace rv = ranges::view;

auto triples() -> shared_generator<std::tuple<int, int, int>> { 
    for (int z = 1; true; ++z) {
        for (int x = 1; x < z; ++x) {
            for (int y = x; y < z; ++y) {
                if (x*x + y*y == z*z) {
                    co_yield std::make_tuple(x, y, z);
                }
            }
        }
    }
}

int main() {
    for (auto&& triple : triples() | rv::take(10)) {
        printf(
            "(%d,%d,%d)\n",
            std::get<0>(triple),
            std::get<1>(triple),
            std::get<2>(triple)
        );
    }
}

这是新的 C++20 特性,还是 Godbolt 的扩展,或者完全不同的东西?

据我所知,#include 指令中“<”和“>”字符对之间的标记的处理方式完全(?)实现定义。

来自 [cpp.include]/2(强调我的)...

A preprocessing directive of the form

# include < h-char-sequence > new-line

searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

话虽如此,这不是我以前遇到过的事情。

这是 godbolt.org 的一个功能。参见 https://github.com/mattgodbolt/compiler-explorer/wiki/FAQ

Q: Can I include a file from an url?

A: Compiler Explorer has the ability to include raw text to your source, by abusing the #include directive.

#include <url_to_text_to_include>
...

(See this link a live example: https://godbolt.org/z/Pv0K0c)

Note that the URL has to allow for cross domain requests for this to work.