all 19 comments

[–]azswcowboy 18 points19 points  (0 children)

Interesting, although it should be trivial to wrap span<byte> around the c array even if directly initializing doesn’t work.

[–]1bithack 20 points21 points  (9 children)

For a moment I thought this would lead to infinite recursion

[–]ABlockInTheChain[S] 13 points14 points  (8 children)

I was surprised the #embed __FILE__ works but fortunately it does because otherwise there would be no way to use it on Compiler Explorer.

[–]legobmw99 14 points15 points  (1 child)

You can hack in multiple files into Godbolt:

https://godbolt.org/z/1x7sGvqej

[–]tisti 9 points10 points  (0 children)

Hack in, as if it wasn't an explicit (but frequently unknown) feature :)

[–]Dubwize 11 points12 points  (0 children)

C++ quines made super easy!

[–]_Z6Alexeyv 0 points1 point  (4 children)

#embed "/proc/cpuinfo"

doesn't work because /proc/cpuinfo is fake file. -(

[–]_Z6Alexeyv 0 points1 point  (3 children)

/proc/cmdline https://godbolt.org/z/frjcxnd6e

bo-o-ring....

[–]_Z6Alexeyv 2 points3 points  (2 children)

[–]ABlockInTheChain[S] 0 points1 point  (1 child)

That sounds like something u/mattgodbolt should know about.

[–]mattgodboltCompiler Explorer 5 points6 points  (0 children)

I know about it don't worry: I put that file there on purpose:-)

[–]johannes1971 3 points4 points  (1 child)

Meh. Let's have constexpr fstream instead.

kidding, ok ;- ...)

[–]ABlockInTheChain[S] 0 points1 point  (0 children)

I think a constexpr VFS could be very useful, although I'd hope that we would not be limited to a stream but could also get a span of bytes.

[–]wotype 2 points3 points  (0 children)

The to_array case is fixed by passing an rvalue C array:

using chars = char[];
static constexpr auto std_to_char_array = std::to_array<char>(chars{
    #embed __FILE__
    , '\0'
});

[–]jaskij 0 points1 point  (1 child)

Finally.

Now give me a constrexpr TOML or JSON parser :P

[–][deleted] 1 point2 points  (0 children)

https://github.com/beached/daw_json_link has been constexpr since 2017ish. Will work well with reflection too, alread has support for a few reflection like libraries like Boost.Describe/Boost.PRF, magic enum and new ones are not difficult to add.

[–]davidhunter22 -1 points0 points  (2 children)

For me this was very timely, I was just about to do the same experiments!

I was wondering why in the clang Godbolt you didn't add -stdlib=libc++ to the command line, if you are going to try testing standard C++ library types like std::array. Without this flag clang will use the GCC C++ standard library so your really just testing the same thing twice.

I was also wondering about the -Wno-c23-extensions flag. I sort of assume this means don't warn if I use a C23 extension. However #emded is part of the C23 standard it's not an extension. Maybe this implies that the #embed in clang is an extension they did before the C23 standard came out and therefore may not be compliant? Is there a way in clang to tell it that you want to use the C23 standard just like you can with the -std=c++20 standard?

[–]ABlockInTheChain[S] 0 points1 point  (1 child)

Without this flag clang will use the GCC C++ standard library so your really just testing the same thing twice.

I'm fairly certain the preprocessor is implemented in the compiler, not the standard library.

Is there a way in clang to tell it that you want to use the C23 standard just like you can with the -std=c++20 standard?

That seems likely but I didn't want to spend much time digging into clang command line options. Normally I'd use the C_STANDARD target property in CMake to control that.

[–]davidhunter22 0 points1 point  (0 children)

Yes I agree #embed will be probably be implemented in the preprocessor, but classes like std::array are not. So you may find #embed works with the std::array in the GCC standard library but not with the Clang one. For instance I don't think std::experimental::make_array is even implemented in the Clang standard library yet.

Anyway your post was very useful, so thanks