You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This feature allows users to implement code that warns about values defined in a table but never used.
872
+
873
+
```cpp
874
+
#include<toml.hpp>
875
+
876
+
namespaceyours
877
+
{
878
+
879
+
Config read_config(const toml::value& input)
880
+
{
881
+
const auto cfg = read_your_config(input);
882
+
883
+
for (const auto& [k, v] : input.as_table())
884
+
{
885
+
if (!v.accessed())
886
+
{
887
+
std::cerr << toml::format_error("value defined but not used",
888
+
v.source_location(), "not used");
889
+
}
890
+
}
891
+
return cfg;
892
+
}
893
+
} // namespace yours
894
+
```
895
+
896
+
This feature is useful when a value is mistakenly defined under the wrong name but is never accessed. For example:
897
+
898
+
```toml
899
+
# The correct key is "reactions"
900
+
# reactions = [ ":+1:", "star" ]
901
+
902
+
# This key is incorrect and will not be read
903
+
reaction = [ ":+1:", "star" ]
904
+
```
905
+
906
+
If this file is read using the above code, `read_your_config` will search for `reactions`. Since it is not defined, it will process `reactions` as an empty array.
907
+
In this case, `input.at("reaction").accessed()` will be `false`, allowing it to be detected as an error.
0 commit comments