static if - is the 'static' really needed?
comco
void.unsigned at gmail.com
Fri Dec 13 05:05:56 PST 2013
On Friday, 13 December 2013 at 12:50:01 UTC, Nicolas Sicard wrote:
> On Friday, 13 December 2013 at 12:10:02 UTC, comco wrote:
>> Imagine a world in which a simple 'if' has the semantics of a
>> static if, if the condition is evaluable at CT. Is this a
>> world you would rather live in?
>>
>> template Fac(int i) {
>> if (i == 0) { // static if; doesn't introduce scope
>> enum Fac = 1;
>> } else {
>> enum Fac = i * Fac!(i-1);
>> }
>> }
>>
>> // If the condition is not evaluable at CT, the ordinary
>> runtime if semantics (introducing scope) are used.
>>
>> Me:
>> pros: simpler syntax
>> cons: harder to reason about; I recall Andrei's talk about the
>> static if proposal to C++: "we don't need _static else_" --
>> why do we even need 'static' in 'static if' by this reasoning?
>
> What would happen when the condition is sometimes evaluable at
> compile time and sometimes not?
>
> void foo(alias a)() {
> /* static */ if (a)
> int x = 1;
> else
> int x = 42;
> doSomethingWith(x);
> }
Multiple versions of the foo function - depending on the caller -
as it is with templates? Static ifs already can produce a
combinatorial explosion of different method implementations :)
At least in the places where declarations are expected and normal
if doesn't make sense: like template/class/struct body, if can be
implicitly static. Now it is implicitly static right after
function / class / template declarations.
class A(T) {
if (is(T : B))
B b;
else
T b;
...
}
More information about the Digitalmars-d-learn
mailing list