[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Using gperf to allow switch-case over strings (in c++17)

From: Viktor Engelmann
Subject: Using gperf to allow switch-case over strings (in c++17)
Date: Sun, 26 Dec 2021 13:58:57 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.1

Hello everyone,

My name is Viktor Engelmann, this is my first message to this mailinglist.

I'm a C++ developer and tried using gperf for generating code that would allow me to write a switch-case over strings. This didn't work at first, so I dug into the source that gperf had generated and added "constexpr" declarations in a few
places. With that, my switch-case over strings worked! (requires c++17)

So then I went to the gperf sources and added an option %constexpr that will generate these "constexpr" declarations. In the attachment, you will find a patch for the latest git revision of gperf, which adds this functionality.

The resulting gperf translates the attached test.gperf to a c++17 source code that ALMOST compiles: You still get one compiler-error, because the user-function "foo" calls the private method "Perfect_Hash::hash". If you manually make that method public, the code compiles, including the switch-case over strings!

        g++ --std=c++17 -o test test_public.cpp

I want to address this compiler-error in a future patch, where I want to add this function "foo" (with a better name) as a public method to the generated class, so that the accessibility of Perfect_Hash::hash isn't a problem anymore - but
I figured that this should be a separate patch.

Kind regards


Attachment: test.gperf
Description: Text document

Attachment: test_public.cpp
Description: Text Data

Attachment: test.cpp
Description: Text Data

Attachment: gperf_constexpr.diff
Description: Text Data

reply via email to

[Prev in Thread] Current Thread [Next in Thread]