The documentation of std::numeric_limits<T>
says it should not be specialized for non-fundamental types. What about number-like user-defined types? If I define my own type T
which represents a numeric value and overloads numeric operators, and for which the information represented by numeric_limits
makes sense -- will anything break if I specialize numeric_limits
for that type?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- How do I get from a type to the TryParse method?
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
Short answer:
Go ahead, nothing bad will happen.
Long answer:
The C++ standard extensively protects the
::std
namespace in C++11 17.6.4.2.1, but specifically allows your case in paragraphs 1 and 2:The older C++03 has a similar definition in 17.4.3.1/1:
After getting past this fundamental stepping stone, you already pointed out, C++03 18.2.1/4 forbids specializations of
::std::numeric_limits
for certain types:The more current C++11 18.3.2.1/4 has a slightly different wording:
Both of these formulations however allow specializations for non-standard types, which
T
is, since you defined it yourself (as @BoPersson already pointed out in the comments).Caveats
C++11 18.3.2.3/1 hints that you should (but does not require you to) ensure that your specialization has all members.
Also, you may wish to ensure that C++11 18.3.2.3/2 is not violated by your specialization:
Which essentially means, that if you wish to specialize it for
T
, you should also do so forT const
,T volatile
andT const volatile
.Just an example:
You may not specialize std::numeric_limits for a user-defined type. A user defined type is not an arithmetic type. There is clear written in the C++ Standard that
and for example