Suppose I create my own floating point type MyFloatingPoint
(e.g. to provide increased precision relative to the built-in types). Is it permissible to specialize the constants in the C 20 <numbers>
header for this type, or is this undefined behavior?
CodePudding user response:
Yes, but only if the specialization depends on a program-defined type (rather than only built-in / standard library types).
See [math.constants]/2 in the post-C 20 draft of the C standard.
Note that without such a specific permission in the standard, it is generally not allowed to specialize a standard library variable template. See [namespace.std]/3.
CodePudding user response:
According to C 20 draft it seems fine:
26.9.2 (...) 2. Pursuant to 16.5.4.2.1, a program may partially or explicitly specialize a mathematical constant variable template provided that the specialization depends on a program-defined type
CodePudding user response:
No it isn't undefined behavior (be careful when to use that term). I don't see any big issues with that (specialy not after reading the other comments).
#include <numbers>
struct my_floatingpoint_t
{
double value;
};
namespace std
{
namespace numbers
{
template<>
constexpr my_floatingpoint_t pi_v<my_floatingpoint_t>{3.14};
}
}
int main()
{
auto pi = std::numbers::pi_v<double>;
auto my_pi = std::numbers::pi_v<my_floatingpoint_t>;
return 0;
}