
On Tue, Mar 15, 2011 at 12:16, Scott McMurray <me22.ca+boost@gmail.com> wrote:
On Tue, Mar 15, 2011 at 11:48, Thomas Klimpel <Thomas.Klimpel@synopsys.com> wrote:
[...] Two of these occurrences had the form "(m%std::ptrdiff_t(n)+n)%n", which surprised me a bit. When I replaced them with "modulo(m, n)", the compiler refused to compile it. So I wrote "modulo(m, std::ptrdiff_t(n))" instead, and this compiled fine. Thinking a bit about this, I realized that the template does the right thing by refusing to compile this, whereas the "(m%n+n)%n" construct can lead to surprising results in case n is of type std::size_t.
Perhaps the real thing it's telling you is that you want something like this:
[snip wrong code]
Because if you really want modulo 4000000000, casting to ptr_diff_t will give very strange results on LP32...
Uh, that second enable_if_c should of course be disable_if_c: template <class T> enable_if_c<numeric_limits<T>::is_signed, T>::type modulo(T m, const T& n) { m %= n; if (m < 0) m += n; return m; } template <class T> disable_if_c<numeric_limits<T>::is_signed, T>::type modulo(T m, const T& n) { m %= n; return m; } Oops, ~ Scott