7 ms·
TOML for Modern C++
- haolez 7y agoI don't get why C++ programmers like to keep the namespace prefixes around. I'd just add lots of "using ..." statements and make my code more readable. What's the chance of conflicts anyway when you are already dealing with a tiny fraction of the code in the current file?
- rlp 7y agoIt's a header-only library, it would effectively add "using" statements anything that included the library.
- haolez 7y agoOuch. Seems like an unsolvable limitation at this point. Nice catch.
- colejohnson66 7y agoC++20 introduces “modules” which are an attempt at alleviating the problem of headers. Support, though, is lacking in major compilers’ master branch. GCC supports it if you build a certain branch from source though.
- LessDmesg 7y agoSome really prompt bugfixing on C++ committee's part. "We have a broken compilation process that leads to humongous build times and namespace pollution! Oh, OK, let's wait another 20 years to fix it". I guess C++ coders deserve the std::'s their language gives them.
- Reelin 7y agoIt's actually possible to avoid this! namespace foo::_bar { using namespace ::acme::widgets; /* internal helpers go here */ inline namespace exports { /* public API goes here */ } } namespace foo::bar { using namespace _bar::exports; }
- mikepurvis 7y agoBesides the header issue mentioned in the sibling, sometimes the context is important too, for an example from Python, no one would `from os.path import join`— you always use `os.path.join` in its entirety.
- deleted 7y ago[deleted]
- tomjakubowski 7y agoI agree with your point and I'm just nit picking, but `from os import path` and `path.join` is pretty acceptable too.
- david2ndaccount 7y agoI have definitely seen `from os.path import join` in some ugly ML github repos.
- pfranz 7y ago:shrug: I've done that quite a bit. This randomly selected popular Python library does it, too[1]. The example for `os.walk` literally has `from os.path import join, getsize`[2]. The docs seem to back this up[3]: "Remember, there is nothing wrong with using `from package import specific_submodule!` In fact, this is the recommended notation unless the importing module needs to use submodules with the same name from different packages." I haven't dealt with a lot of C++ code but I feel like I see a lot of `using namespace ...` and haven't seen much `using ...;` (which I think is more readable/acceptable) [1] https://github.com/psf/requests/blob/df918c066fa275abc2bb0c960647dc064d136a01/requests/utils.py#L22 https://github.com/psf/requests/blob/df918c066fa275abc2bb0c9... [2] https://docs.python.org/3/library/os.html#os.walk https://docs.python.org/3/library/os.html#os.walk [3] https://docs.python.org/3/tutorial/modules.html#importing-from-a-package https://docs.python.org/3/tutorial/modules.html#importing-fr...
- eska 7y agoYou could do that in .cpp files, but it's objectively bad to do so in the header files due to namespace pollution. And now you have an inconsistency between headers and .cpp files..
- LessDmesg 7y agoWhy do header files even exist and why do sane languages work without them? Oh, I forgot, you cannot question The Holy Warts of Stroustrup. It's just C++. Leave all hopes of developer productivity at the door.
- pansa2 7y ago> C++17 > Works with or without exceptions Is anyone using C++17 features with exceptions disabled? I thought the “no exceptions” people were generally only using C++ as C-with-classes?
- cmrdporcupine 7y agoAll of Google's codebase, including Chromium, is OO C++ without exceptions. https://google.github.io/styleguide/cppguide.html#Exceptions https://google.github.io/styleguide/cppguide.html#Exceptions
- userbinator 7y agoIn other words, that codebase is without exceptions, without exception.
- DoofusOfDeath 7y agoExceptional observation.
- saagarjha 7y agostd::bad_alloc would like to have a word with you.
- MHordecki 7y agoAn exception that proves the rule.
- pansa2 7y agoThanks - I’d seen that section before, but hadn’t noticed that elsewhere the document says “code should target C++17“. Do you know if there are any parts of the C++17 language or standard library that are disallowed in Google’s code because those parts don’t work without exceptions?
- 7y ago
- usefulcat 7y agoWhat are the advantages of this library over, say https://github.com/ToruNiina/toml11 https://github.com/ToruNiina/toml11 ?
- marzer 7y agoI actually tried using toml11 before writing toml++, and my main gripe with it was the complexity. It does a _lot_, and I found it to be pretty unergonomic to actually use. That, and it's not as configurable (e.g. no support for exception-less).
- rpasquay 7y agoI'm a bit surprised by this snippet from the "Traversing and manipulating data" example: for (auto& elem : *arr) { // visitation helps deal with the polymorphic nature of TOML data elem.visit([=](auto&& el) noexcept { if constexpr (toml::is_number<decltype(el)>) (*el)++; else if constexpr (toml::is_string<decltype(el)>) el = "five"sv; }); } I thought constexpr would work at compile time but how can the compile here inside a loop "know" when this labmda will encounter an int or a string ?
- jdashg 7y agoIt's tricky but I think visit is being passed a generic (templated) lambda, which is statically instantiated for each type that a node might be. (Once for int, once for string, etc) Since the type is static per template instantiation, constexpr works here.
- TuxSH 7y agoHm, it looks like it is using things like std::vector and streams, etc. So it does actually require exceptions and RTTI/still forces linkage against throwing functions, etc.
- marzer 7y agoSure, if you want to be pedantic: it works without them when paired with standard implementations that can work without them
- jdashg 7y agoIn no-exception builds, exceptions become crashes. RTTI is barely used anywhere in the STL. As an example, Firefox uses STL without RTTI or exceptions.