boostorg / property_tree

Boost.org property_tree module
http://boost.org/libs/property_tree
55 stars 92 forks source link

Prevent calls to get_child with temporary default values #115

Closed ashtum closed 10 months ago

ashtum commented 10 months ago

Resolves #42

ashtum commented 10 months ago

@pdimov, do you have any suggestions on how to add a test to verify that get_child doesn't compile with temporary default values? Can I use a concept and include it as a test for C++20 and higher? (although it doesn't look elegant)

pdimov commented 10 months ago

Use a compile-fail test.

ashtum commented 10 months ago

Use a compile-fail test.

Just out of curiosity, is there any alternative way to test it without using a separate compilation unit? I mean, in complex scenarios, the compilation failure could be due to other reasons as well.

pdimov commented 10 months ago

There probably is but I wouldn't recommend it.

pdimov commented 10 months ago

Something like https://godbolt.org/z/j7MaTGTcd.

ashtum commented 10 months ago

Something like https://godbolt.org/z/j7MaTGTcd.

Do you discourage it because of the possibility of the expression becoming invalid for other reasons and not manifesting itself in the test? If that's the case, can't we mitigate this by also testing valid expressions, like so:

static_assert(mp_valid<accepts_default_of_type, ptree&>::value, "");
static_assert(mp_valid<accepts_default_of_type, const ptree&>::value, "");
pdimov commented 10 months ago

It's too complicated, requires specialized knowledge to understand, and is therefore hard to maintain.