tomlplusplus icon indicating copy to clipboard operation
tomlplusplus copied to clipboard

Question about .is_array_of_tables()

Open levicki opened this issue 3 months ago • 6 comments

Not a bug report, a (hopefully) simple question.

If I have:

[[MyConfig]]
TimeStamp = 1972-11-21T11:30:00+01:00

[[MyConfig]]
TimeStamp = 1979-05-27T00:32:00-07:00

Am I wrong if I expect Value.is_array_of_tables() should return true?

Currently it returns Value.is_array() as true.

Is that perhaps reserved only for things like:

[Section]
Value = [ { First = 1, Second = 2 }, { Whatever = 3, Life = 42 } ]

If not, can you please give an example TOML for which Value.is_array_of_tables() returns true?

Please advise.

levicki avatar Nov 10 '25 17:11 levicki

The MyConfig node in the config above would yield true for is_array_of_tables(). (Or it should, anyway.)

marzer avatar Nov 10 '25 18:11 marzer

Maybe it's both and the order of checking is important? Currently I first test for array then for array of tables. Apparently I should try the other way around.

levicki avatar Nov 11 '25 00:11 levicki

Order shouldn't matter; the is_array_of_tables() function is just a shorthand for val.is_array() && val.is_homogenous<table>(). Both MyConfig and Section.Value in your examples above should yield true.

marzer avatar Nov 11 '25 10:11 marzer

Aha, so for array of tables both is_array and is_array_of_tables are true. I kind of didn't expect that (because I thought is_array was only true for simple types) so it turns out that the order of comparison matters.

levicki avatar Nov 11 '25 11:11 levicki

Ah, I see the confusion. toml++ doesn't deserialize a special array-of-tables type; it literally just creates a regular array and populates it with each table, hence is_array being true (which makes sense; an array of tables is an array).

marzer avatar Nov 11 '25 11:11 marzer

I am struggling to implement runtime reflection for deserialization directly into classes like in C# so that's how I hit this ambiguity. I kinda got a simple working solution for standard types and even arrays and I can deserialize flat tables and even nested tables but arrays of tables? Nope.

I wish C++ had something like this:

int a = 42;
void *v = &a;
std::type_info t = typeid(int);
int *p = typeid_cast<t>(v);

Everything would've been so much easier.

Either that or some way to create an instance of a value type if you know the outer type. Guess I will have to wait until C++26.

levicki avatar Nov 11 '25 22:11 levicki