#std:visit can I somehow tell the compiler which types to skip?

33 messages · Page 1 of 1 (latest)

waxen sandal
#
template <size_t s0, size_t s1, typename t0, typename t1>
class ExampleClass {};
template <size_t s0, typename t0>
struct ExampleStruct {};
using type_variant = std::variant<ExampleClass<1, 1, size_t, size_t>, ExampleStruct<1, size_t>>;
std::array<type_variant, 2> allTypes =
{
    type_variant{ExampleClass<1, 1, size_t, size_t>{}},
    type_variant{ExampleStruct<1,size_t>{} }
};
template <size_t s0, size_t s1, typename t0, typename t1>
struct TypeInfo<ExampleClass<s0, s1, t0, t1>> {
    static constexpr size_t ID = 1;
    static constexpr size_t count_S = 2;  // s0, s1
    static constexpr size_t count_T = 2;  // t0, t1
    static constexpr const char* name = "ExampleClass";
};
template <size_t s0, typename t0>
struct TypeInfo<ExampleStruct<s0, t0>> {
    static constexpr size_t ID = 2;
    static constexpr size_t count_S = 1;  // s0
    static constexpr size_t count_T = 1;  // t0
    static constexpr const char* name = "ExampleStruct";
};




    std::visit([&](const auto& v1_) {
        using v1__1 = std::decay_t<decltype(v1_)>;
        v1__1 obj_1{};
        if (TypeInfo<v1__1>::ID == 2)
        {
            auto lambda = [&]<template<size_t, typename> typename Class, size_t N, typename T>(Class<N, T>&obj)
            {
                std::visit([&](const auto& v1_v1_) {
                    std::visit([&](const auto& v1_v2_) {
                        integer_visit([&](auto v1_s1_) {
                            integer_visit([&](auto v1_s2_) {

                                size_t one4 = foo<v1_s2_>();

                                Class<v1_s2_, float> a;
                                std::cout << typeid(a).name();

                                }, v1_s2);
                            }, v1_s1);
                        }, v1_v2);
                    }, v1_v1);
            }; lambda(obj_1); // this doesnt work for all type_variants what should I do?
        }
    }, v1);
}


ruby lynxBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

waxen sandal
#

with that if statement in the middle I try to make this code only apply to one of the variants.

#
 if (TypeInfo<v1__1>::ID == 2)
#

My thinking is atm 3 options:

1 : Somehow breaking up the variant into subvariants?

2: using tuple and getting a specific type

3: trying to make the lamda more variable : adaptable

#

..
a keyword might be std::variant_alternative i am trying to read about it

gritty scarab
#

What are you trying to do with this?

waxen sandal
#

At the core: I want to call a lambda function for each of my types

auto lambda = [&]<template<size_t, typename> typename Class, size_t N, typename T>(Class<N, T>&obj)
{
    Class<3,float> // <- I can remove the class type from the template parameter:
} lambda(obj);
#

atm It works if all the variants have the same number of template parameters

#

and it fails when they have a differnt number of parameters

#

I think it is because when visiting a variant some variant members are not compatable.

#

this is the functioning code:

int main()
{
    Go();
    return 1;

}
#
void Go()
{
    //----------------------------------------------------------------- we can use those to prepare some data
    size_t id = 0;
    size_variant id_first_type = id;
    integer_visit([&](auto id_first_type_)
        {
            using first_type = std::variant_alternative_t<id_first_type_, type_variant>;
            first_type obj = first_type{};
            using v1__1 = std::decay_t<decltype(obj)>;
            v1__1 obj_1{};
            std::cout << typeid(first_type).name();
            std::cout << std::endl;
            auto lambda = [&]<template<size_t, typename> typename Class, size_t N, typename T>(Class<N, T>&obj)
            {
                //Class // <- I can remove the class type from the template parameter:
            };
            lambda(obj);
        }, id_first_type);
}
#

I want to upgrade it so that my variant can have templates of with any parameters now its fixed to two

gritty scarab
#

Okay, sorry, I might be missing something. But can't you use std::visit directly?

#
auto lambda = [&]<template<size_t, typename> typename Class, size_t N, typename ...T>(Class<N, T...>&obj)
{
    
};
#

If you just make the lambda variadic

waxen sandal
#

Yes if the classes had only one size_t and any typenames.

#

Which i might just make a rule...

#

... hmm i think I'll make it a rule ...

Only exception is when there are types that don't have size_t
I should make it work for types that are non template too...

waxen sandal
gritty scarab
#

I don't understand what you're doing. If you went with [&](const auto &obj), that would of course accept anything, but wouldn't give you any information about the template parameters if any

#

But you can extract them with some other approach

waxen sandal
#

What other approach would come to mind?

#

I feel it is hard getting the ' Class' removed from it's template parameters.

gritty scarab
#

I don't understand the requirements, can you say again what you need?

#

Say ExampleStruct<42, int> gets passed. What do you need from it?

waxen sandal
#

The
'ExampleStruct'
Without the parameters
So one can do
ExampleStruct<3,float>

waxen sandal
#

But i think i will try some things and then get back with better data.
Thank you for your help.

ruby lynxBOT
#

@waxen sandal Has your question been resolved? If so, type !solved :)

gritty scarab
#

It's impossible to make this fully general

#

Because there are no universal template parameters (that can accept both types and nontypes)