简体   繁体   中英

Is it allowed to extend the std::numbers namespace with new definitions?

I have several mathematical numeric constants defined in a large codebase. Several of which (but not all) are now duplicated in the new C++20 <numbers> header. I'd like to have them all in one place; is it allowed to extend the std::numbers header to include the ones not already defined?

is it allowed to extend the std::numbers header to include the ones not already defined?

No, you may not add definitions to std namespace nor its subnamespaces (except for class template specialisations in cases where that isn't explicitly disallowed).

You can instead have them all in one place in your own namespace with using declartions:

namespace Casey
{
    inline constexpr double missing_number = 123;

    using std::numbers::e;
    // ...
}

Instead of using each number individually, you could use using namespace std::numbers , but that has the caveat that future standard versions may add numbers whose names may potentially conflict with yours, breaking future compatibility of your header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM