run std::visit on the variant and original type

Have a strange case where sometimes a function takes a variant but other times it takes the member of the variant.

void branching_function(auto argument)
{
    using typer =  std::remove_reference<decltype(argument)>::type;
    using variant_type = std::variant<int,double>;
    // using variant_type = std::variant; <- doesn't work because missing <int, double> (here we could use a Concept over std::variant?)
    if constexpr(std::is_same<typer, variant_type>::value)
    {
        // std::variant action (do visit)
        std::visit([](auto argument_s){
            (void)argument_s;
        },argument);
    }
    else
    {
        //non std::variant action (don't do visit)
    }
}

https://godbolt.org/z/xT1KcdK11

I can use a if constexpr + std::is_same to test if i'm on the variant case and get the expected behavior.

I'm struggling to figure out how to generalize the std::is_same test so that the condition applies to all std:: variants.

Is there some kind of "is visitable" concept in C++?



Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)