Overloading opEquals(T)(T y)

Steven Schveighoffer schveiguy at yahoo.com
Tue Feb 8 09:14:05 PST 2011


On Tue, 08 Feb 2011 11:49:47 -0500, spir <denis.spir at gmail.com> wrote:

> On 02/08/2011 05:17 PM, Steven Schveighoffer wrote:

>> What is the difference between:
>>
>> bool opEquals(T)(T y) if (is(T == BigRational)) {...}
>>
>> and
>>
>> bool opEquals(T)(T y)
>> {
>> static if(is(T == BigRational)) {...}
>> else static assert(0, "invalid type");
>> }
>
> Thank you very much, Steven.
> I'd say: in the first case, there are as many functions as constraints  
> variants (provided other variants are implemented, indeed) triggered by  
> actual calls to opEquals.
> Tell me if i'm right on this: In the second case, there is a single  
> function generated, and branches of static if are inserted according to  
> actual calls. Is that right? If yes then I don't really care of this use  
> of static f (I'm aware there are other, maybe more relevant, use cases).

No, there are multiple functions generated, and in each function, the  
correct static if branch is compiled.  It's technically no different  
code-generation wise than the constraint version.

The difference comes in the compiler decision of whether to compile a  
function or not, and who is responsible for reporting the error (in the  
first, the compiler reports the error, in the second, you are responsible  
for reporting the error via static assert).

-Steve


More information about the Digitalmars-d mailing list