How to add a string literal annotation to items?

6 days ago 8
ARTICLE AD BOX

I am experimenting with the new annotation freshly added in C++26, specifically looking at the examples provided in the proposal paper.

The paper contains the following example using a hypothetical clap library for argument parsing:

struct Args { [[=clap::Help("Name of the person to greet")]] [[=clap::Short, =clap::Long]] std::string name; [[=clap::Help("Number of times to greet")]] [[=clap::Short, =clap::Long]] int count = 1; }; int main(int argc, char** argv) { Args args = clap::parse<Args>(argc, argv); for (int i = 0; i < args.count; ++i) { std::cout << "Hello " << args.name << '\n'; } }

Behind the scenes, the struct definition for the Help annotation looks something like this:

struct HelpArg { const char* msg; };

However, the Godbolt link provided in the paper (https://godbolt.org/z/dM3erErrz) currently fails to compile in Clang. It emits the following error:

error: pointer to subobject of string literal is not allowed in a template argument

This will also fail in the upcoming GCC 16 because C++26 annotation values require the type to be a "structural type", and holding a pointer to a string literal violates the NTTP (Non-Type Template Parameter) rules, afaik.

What is the standard workaround to make string literal annotations work in supporting C++26 compilers?

Read Entire Article