Quantcast
Channel: User Peter - Reinstate Monica - Stack Overflow
Viewing all articles
Browse latest Browse all 223

Answer by Peter - Reinstate Monica for Compile-time string compression with C++17 and earlier

$
0
0

I may have a C++17 example here with the added benefit that it does not store the string literals in the binary. I checked for pretty long arguments (the strings in line 35 pp. in the assembler are the compressed results).

Unlike in your code, the string literal is used to initialize a constexpr array or pointer variable which is then fed to a simplified compressor class. The size has to be computed separately. That's more code per string but still, the literal only appears once. By the looks of it it can be easily generated.

The call side looks like this:

        constexpr const char p[] = "aaaabbbbbbbbbbbbbbbbbbbc";        constexpr std::size_t comprSz = compress(p, sizeof(p), nullptr);        constexpr Compressor<comprSz> cpr{p, sizeof(p)};        print(cpr.comprData, comprSz);

It would be possible to move the size computation into another intermediary class but it does not seem possible, for whatever reason, to use plain vanilla string literals as template parameters so that the call can be a single statement, like with your user literal.


Viewing all articles
Browse latest Browse all 223

Trending Articles