static_read

161 views
Skip to first unread message

ma.ka...@web.de

unread,
May 20, 2017, 2:32:28 PM5/20/17
to ISO C++ Standard - Future Proposals
Hallo guys,

my idea is about the possibility to read in file at compile time using static_read.
The file content ist returned as a string literal, which can be processed afterwards.
Important is, that the the returned string literal is constexpr. You can use it as
template parameter or inside constexpr functions.
What is it good for?

Think of an BNF or EBNF grammer described in a external file. This file is
transformed into a string literal. The string literal could be transformed into a parser
at compile time, using a contexpr function.

This is not limited to BNF or EBNF. It works for all kind of domain specific languages.
Another usage are data files. ll kind of xml files could be transformed
to C++ Data structures, for example.

MFG

Martin

Thiago Macieira

unread,
May 20, 2017, 3:30:55 PM5/20/17
to std-pr...@isocpp.org
What's wrong with using a code generator tool that reads the file and produces
a header with the constexpr string?

--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center

Peter Sommerlad

unread,
May 20, 2017, 3:39:38 PM5/20/17
to std-pr...@isocpp.org
"\
#include <file>\
"
?
send from a mobile device.
Prof. Peter Sommerlad
peter.S...@hsr.ch
+41-79-432 23 32
> --
> You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
> To post to this group, send email to std-pr...@isocpp.org.
> To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/12463358.l9y6zcXXmN%40tjmaciei-mobl1.

Arthur O'Dwyer

unread,
May 21, 2017, 12:56:52 PM5/21/17
to ISO C++ Standard - Future Proposals, ma.ka...@web.de
On Saturday, May 20, 2017 at 11:32:28 AM UTC-7, ma.ka...@web.de wrote:
Hallo guys,

my idea is about the possibility to read in file at compile time using static_read.
The file content ist returned as a string literal, which can be processed afterwards.
Important is, that the the returned string literal is constexpr. You can use it as
template parameter or inside constexpr functions.

Coincidentally, this topic just came up in conversation yesterday after a couple of years of not thinking about it. Strange synchronicity. :)
Here's a proposal on the subject from May 2016:

To Peter's question: no, you can't just put an #include directive inside a string, because "inside a string" there are by definition only characters.
"\
#include <foo>\
"
is equivalent (after backslash-replacement) to
"#include <foo>"
which doesn't have anything to do with a file named "foo" as far as the compiler is concerned.

So the above-linked proposal — which was extensively discussed on this very mailing list; maybe you can dig up the thread(s) — proposed that we introduce a completely new kind of string literal: F"foo" means "open the file named foo and dump its contents into this string."

I don't know whether the proposal was ever discussed in Committee. Personally I think it has two major hurdles to overcome:
- it's a preprocessor feature request, and changing the preprocessor sucks (because C compatibility; because modules)
- it's proposing a completely new dimension of stringness, on top of the two dimensions we already have

Consider: if F"foo" is the contents of file foo, what is u8F"foo"? what is LF"foo"? In short, what is the interaction between the C++ notions of "character set" and "encoding" with the filesystem concept of "the text stored in this file"?  — Please note that this horse was absolutely beaten to death in the old thread, so I do recommend that you go dig it up and read it. But, for people who aren't the OP and yet might still want to debate the topic, please do read the proposal P0373r0, since it has a lot to say on the subject and can get you up to speed on the issues relatively quickly.

HTH,
–Arthur

ma.ka...@web.de

unread,
May 22, 2017, 2:37:38 AM5/22/17
to ISO C++ Standard - Future Proposals, ma.ka...@web.de
Really interesting. A nice idea to introduce a new kind of string literal. Thanks for the hint.

ol...@join.cc

unread,
May 24, 2017, 4:58:43 AM5/24/17
to ISO C++ Standard - Future Proposals, ma.ka...@web.de


Op zaterdag 20 mei 2017 20:32:28 UTC+2 schreef ma.ka...@web.de:
Hallo guys,

my idea is about the possibility to read in file at compile time using static_read.
The file content ist returned as a string literal, which can be processed afterwards.
Important is, that the the returned string literal is constexpr. You can use it as
template parameter or inside constexpr functions.
What is it good for?


Reply all
Reply to author
Forward
0 new messages