Member function increases size of a struct defined in a function - is this a bug?
Dennis
dkorpel at gmail.com
Tue Feb 11 13:04:01 UTC 2020
On Tuesday, 11 February 2020 at 11:32:47 UTC, Max Samukha wrote:
> Sorry for the late reply. I agree it is not a big issue. On the
> other hand, to draw an (probably far-fetched) analogy with C++
> structs, I would be surprised if the compiler added a vtable
> pointer to the struct in the absence of virtual functions.
The thing is, because of D's static introspection and
order-independent declarations things can get very complicated
very quick. Here is an example that can be compiled two ways:
```
import std;
void main() {
string str = "from closure";
struct S {
int b;
string method() {
static if (S.sizeof > int.sizeof) {
return str; // current behavior
} else {
return "constant"; // also a valid option
}
}
}
writeln(S.init.method);
}
```
Of course you can account for paradoxical cases like this, and
there are cases where the compiler already does:
```
struct S {
int a = 3;
static if (S.sizeof == int.sizeof) {
int b = 3;
}
static if (S.sizeof == int.sizeof) {
int c = 3;
}
}
```
> variable onlineapp.S.c cannot be further field because it will
> change the determined S size
But the takeaway is: even if you make a decision a bit more
intelligently, it can increase compiler complexity in unexpected
ways.
More information about the Digitalmars-d
mailing list