Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
408 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...