C++ Logo

std-proposals

Advanced search

Re: Feature test macro for variant<bool, string> taking const char* fix?

From: Emile Cormier <emile.cormier.jr_at_[hidden]>
Date: Sun, 6 Dec 2020 18:44:56 -0400
Okay, perhaps not the perfect example. But the problem is that the behavior
of std::variant changed between C++17 and C++20 and there's no easy way to
tell at compile time which behavior std::variant with exhibit. It's the
principle of the thing.

Your same "why not just leave the workaround code" argument could apply for
many other language and standard library fixes which DO have a feature test
macro (or an increased version of the macro).

To be clear, I'm not asking for programming help (I'd go to Stackoverflow
for that). I'm raising the flag that there's perhaps an omission in the
C++20 spec.

On Sun, Dec 6, 2020 at 6:34 PM Barry Revzin <barry.revzin_at_[hidden]> wrote:

>
>
> On Sun, Dec 6, 2020 at 4:29 PM Emile Cormier <emile.cormier.jr_at_[hidden]>
> wrote:
>
>> I do, Barry. Something like this (not tested):
>>
>> struct parse_event
>> {
>> template <typename T>
>> parse_event(T&& value) : token(std::forward<T>(value)) {}
>>
>> #ifndef VARIANT_SAFELY_TAKES_CHAR_POINTER
>> // Workaround
>> parse_event(const char* s) : token(std::string(s)) {}
>> #endif
>>
>> std::variant<bool, int, std::string> token;
>> };
>>
>> The idea is to disable the workaround code when the C++ library
>> implementation has the P0608/P1957 fixes.
>>
>
> You could just write:
>
> struct parse_event
> {
> template <typename T>
> parse_event(T&& value) : token(std::forward<T>(value)) {}
>
> // Workaround
> parse_event(const char* s) : token(std::string(s)) {}
>
> std::variant<bool, int, std::string> token;
> };
>
> This works regardless of whether the fix is implemented.
>
> Barry
>

Received on 2020-12-06 16:45:12