#include <type_traits>
template <typename I>
struct mround_type
{
static_assert(
std::is_integral<I>::value, "mround_type requires integral type");
const I value;
constexpr explicit mround_type(I v) : value(v) { }
};
template <typename I>
constexpr auto mround(I i)
{
static_assert(
std::is_integral<I>::value,
"mround() parameter must be of integral type");
return(mround_type<I>(i));
}
// Rounded-up integral division.
template <typename I>
constexpr I operator / (I i, mround_type<I> factor)
{
return((i + factor.value - 1) / factor.value);
}
// Round up integer to multiple.
template <typename I>
constexpr I operator + (I i, mround_type<I> factor)
{
return((i / factor) * factor.value);
}
// Round down integer to multiple.
template <typename I>
constexpr I operator - (I i, mround_type<I> factor)
{
return(i - (i % factor.value));
}
static_assert(9 / mround(5) == 2, "");
static_assert(10 / mround(5) == 2, "");
static_assert(11 / mround(5) == 3, "");
static_assert(9 + mround(5) == 10, "");
static_assert(10 + mround(5) == 10, "");
static_assert(11 + mround(5) == 15, "");
static_assert(9 - mround(5) == 5, "");
static_assert(10 - mround(5) == 10, "");
static_assert(11 - mround(5) == 10, "");
I do not quite like the idea of operator + and operator -. It's kind of misleading, that operator + and - has so strange meaning. Why not doing just simple function round_up_to_multiple, which would have cleaner interface?
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/1af197bf-4e85-4e4b-9486-dd7ca150c139%40isocpp.org.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
#include <type_traits>
template <typename I>struct mround_type { static_assert( std::is_integral<I>::value, "mround_type requires integral type");
const I value;
constexpr explicit mround_type(I v) : value(v) { }
// Rounded-up integral division. constexpr I div(I factor) { return((value + factor - 1) / factor);
}
// Round up integer to multiple.
constexpr I up(I factor) { return(div(factor) * factor);
}
// Round down integer to multiple.
constexpr I down(I factor) { return(value - (value % factor));
} };
template <typename I>constexpr auto mround(I i) { static_assert( std::is_integral<I>::value, "mround() parameter must be of integral type");
return(mround_type<I>(i)); }
static_assert(mround(9).div(5) == 2, "");static_assert(mround(10).div(5) == 2, "");static_assert(mround(11).div(5) == 3, "");
static_assert(mround(9).up(5) == 10, "");static_assert(mround(10).up(5) == 10, "");static_assert(mround(11).up(5) == 15, "");
static_assert(mround(9).down(5) == 5, "");static_assert(mround(10).down(5) == 10, "");static_assert(mround(11).down(5) == 10, "");
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5b05d62b-b1ff-42f5-a929-e35d697c4869%40isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/5b05d62b-b1ff-42f5-a929-e35d697c4869%40isocpp.org.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/be7fb134-efa2-4f35-aa0f-51b11efc52da%40isocpp.org.
template <typename T>
struct X
{ explicit X(T t); };
template <typename T>
void foo(X<T> x);
void bar() { foo(X(5)); } // causes an error
template <typename T>
X<T> x(T t) { return(X<T>(5)); }
void bar2() { foo(x(5)); }
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/be7fb134-efa2-4f35-aa0f-51b11efc52da%40isocpp.org.