Boost logo

Boost Users :

Subject: Re: [Boost-users] C++ guru required!
From: Robert Ramey (ramey_at_[hidden])
Date: 2012-02-18 12:31:49

Daryle Walker wrote:
> From:ramey_at_[hidden]
> Date:Fri, 17 Feb 2012 12:40:12 -0800
> I've got a really dumb question. My question is illustrated by the
> followingcode snippet.
> template<class T, class U>T inline operator%(const T & lhs, const U &
> rhs) { if(0 == rhs) throw std::domain_error("Divide by
> zero"); if(boost::is_same<T, boost::uintmax_t>::value &&
> boost::numeric::is_signed<U>::value ){ if(1 == rhs || -1
> == rhs) // is this dropped? overflow("unsigned type can
> hold this result"); } return lhs % rhs;}
> I would like to think that the second if is alwaysdropped from the
> compile for a particular pairof types since this can be evaluated at
> compile time.
> If I'm correct, I can replace some tedioustemplate metaprogramming
> for some straightforwardand transparent code inserted in a convenient
> codeinserted in the most convenient place.
> Andre Alex... gave a talk at "Going Native" proposinga "static if"
> for this case. But I don't see the necessity forfor this since I
> would assume that the compilerjust optimises away the "dead" code.
> I've compiledthe above and it seems to do what I want butstill I
> wonder.
> Basically I see lots of applications of variationson this idea to get
> the benefits of tmp withoutthe attendent pain.
> Am I missing anything here?
> If you want to (partially) specialize on types, then use a class
> template that can act as a function object (i.e. has an operator ()).
> I was going to leave at that, but I see some other problems.

Hmmm - this doesn't answer my question.

My question is: is there any reason that my example is a bad idea.
It works as I expect and is the equivalent of the TMP solution
(assuming that the C++ compiler eliminates dead code - which
is presumed to be widely implemented though not required by
the standard.)

To me it yields all the benefits detailed in Andrei's proposal for "static

The only problem I have with it is that it depends upon widely
implemented behavior which is not required by the standard. questions:

a) is this a problem?
b) are there any other problems which I haven't noticed?

Robert Ramey

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at