C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Preprocessor

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Mon, 14 Aug 2023 11:35:25 +0200
pon., 14 sie 2023 o 11:02 BAMBYK via Std-Proposals
<std-proposals_at_[hidden]> napisaƂ(a):
>
> There is often much of files in a specific directory and write each time directory name before file is annoying and a bit loss readability in some cases.
>
> Yes, you can create a one common file that includes all other. However i consider it is not always suitable. Sometimes you would better include files right where you need instead of creating separate files. It is not always nice to walk throughout such "reference" files. Also write #go and #exit then is the most fast way than those two above.
>
> With this keyword, it seems adds more code structure understand. You right understand what and where is located by just looking at code.
>
>
> And, i could agree if this feature would cause compiler low perfomance. But it doesn't cause perfomance issues and simple to implement.
>
> The example:
>
> #include <vector>
> #include <string>
> #include <regex>
> using namespace std;
>
> vector<string> dirs = {};
> const regex goDefinition(
> R"(^\s*#\s\bgo\b\s*"([^"])"\s*$)"
> );
> const regex exitDefinition(
> R"(^\s*#\s\bexit\b\s*$)"
> );
> const regex inclDefinition(
> R"(^\s*#\s\binclude\b\s*"([^"])"\s*$)"
> );
>
> // imagine file lines
> vector<string> lines = {
> "#go \"output\""
> " // output"
> " #include \"helloWorld.h\""
> " #go \"messages\""
> " // output/messages"
> " #include \"messageData1.h\""
> " #include \"messageData2.h\"
> " #exit"
> "#exit"
> }
>
> for (const string line : lines) {
> smatch match
> if (regex_match(line, match, goDefinition) {
> const string dir = match[1].str();
> dirs.push_back(dir);
> } else if (regex_match(line, match, inclDefinition)) {
> string path = match[1].str();
> // imagine we have join feature
> // which joins array to string by separator
> path = join(dirs, "/") + "/" + path;
> // process include with new path...
> } else if (regex_match(line, match, exitDefinition)) {
> if (dirs.size() == 0) {
> // error
> }
> dirs.pop_back();
> }
> }
>
>
> Now the feature is implemented and can be used.
>
>

You already can do something like this using normal preprocessor:
```
#define M_VEC(X) X ## tor/test
#define M_String2(X) #X
#define M_String(X) M_String2(X)
#include M_String(M_VEC(vec)) //fatal error: 'vector/test' file not found
```

Aside from using abomination like this, the canonical way
to share defions in C++ is now modules, not textual inclusion
of text blobs.

Received on 2023-08-14 09:35:36