Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly access c++/CLI initonly TimeSpan field?

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?

like image 952
Dmitry Nogin Avatar asked Dec 12 '12 02:12

Dmitry Nogin


1 Answers

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.

like image 193
Hans Passant Avatar answered Nov 18 '22 11:11

Hans Passant