C++ Iterate over nested Tables in tomlplusplus

102 Views Asked by At

I'm using tomlplusplus to load my toml configuration. So far this works good. But i'm currently struggle to iterate over nested tables (table arrays). Suppose i have a toml configuration like this:

[[data]]
name = "Data_One"

[[data]]
name = "Data_two"

i tried to iterate over the nested tables like it is mentioned in the Documentation for Arrays but this seems not to work. At least i got some compilation errors.

This is the code that i'm using:

int main() {
    toml::table tbl;
    try {
        tbl = toml::parse_file("config.toml");
        std::cout << tbl["data"] << '\n';
        toml::array* pipes = tbl["data"].as_array();
        pipes->for_each([](auto&& ele) {
            std::cout << ele << '\n';
        });

    } catch (const toml::parse_error& error) {
        std::cout << error << '\n';
    }

    return 0;
}

And the error i got is saying:

error C2338: static_assert failed: 'TOML node visitors must be invocable for at least one of the toml::node specializations: ...
error C3313: 'keep_going': variable cannot have the type 'const void' ...
error C3536: 'keep_going': cannot be used before it is initialized ...

Is there a way to iterate of the nested tables? I saw that the toml::table supports a is_array_of_tables() function but not a as_array_of_tables() conversion function.

0

There are 0 best solutions below