7

I was looking at a random C++ example on 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 ).

#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)
        );
    }
}

Is this a new C++20 feature, or an extension on Godbolt, or something entirely else?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • 2
    It's a Godbolt feature. The examples in my repo are specifically designed to be pasted into Godbolt, and the header files in my repo are specifically designed as "leaf" header files so that they _can_ be included from Godbolt. – Quuxplusone Jul 16 '20 at 17:33

2 Answers2

7

It's a feature of godbolt.org. See 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.

cpplearner
  • 13,776
  • 2
  • 47
  • 72
1

As far as I'm aware the manner in which the token between the '<' and '>' character pair in a #include directive is processed is entirely(?) implementation defined.

From [cpp.include]/2 (emphasis mine)...

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.

Having said that, it's not something I've ever encountered before.

G.M.
  • 12,232
  • 2
  • 15
  • 18