The following code compiles with warning and intellisense error:
ref class Test {
initonly static TimeSpan Delay = TimeSpan(1,1,1);
Test() {
long long ticks = Delay.Ticks; // << problem
}
};
The problems are:
- warning C4395: 'System::TimeSpan::Ticks::get' : member function will
be invoked on a copy of the initonly data member 'Test::Delay'
- IntelliSense: taking the address of an initonly field is not allowed
How to access Ticks properly?
Well, that's pretty major fail-whale. The warning is accurate, the compiler doesn't know enough about the TimeSpan::Tick property getter. It cannot ensure that the getter doesn't do anything that might alter the value of the struct and thereby invalidates the initonly contract. It solves it by making a copy of the struct and warns about it since this is a possible perf issue. That's a bit heavy-handed, other managed compilers make the copy without saying anything about it. I'd just plunk a #pragma warning(disable:4395)
ahead of it so the warning is suppressed.
The IntelliSense error only appears when I try this in VS2012. That's a bug. You can report it at connect.microsoft.com.
Given the misery and that this appears to be a private class member, I'd just drop initonly to get ahead. If that's not desirable then you can wrap the field with a property, intentionally creating the copy, and thus get rid of both problems, like this:
ref class Test {
initonly static TimeSpan _delay = TimeSpan(1,1,1);
static property TimeSpan Delay {
TimeSpan get() { return _delay; }
}
Test() {
long long ticks = Delay.Ticks;
}
};
The jitter optimizer will get rid of it so don't worry about overhead.
Thanks to Hans Passant for his explanation, I understand the context much better now
I have just come across the same problem.
My solution was
- replace the 'initonly' by 'const'
- When accessing, just cast the const away, and there will not be any errors/warnings left
Maybe this is not 'state of the art' coding but it works fine, see here
(VisualStudio2017 does still expose this behaviour/problem)