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

本秂侑毒 提交于 2019-11-27 04:41:59

问题


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?


回答1:


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.




回答2:


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)



来源:https://stackoverflow.com/questions/13831773/how-to-properly-access-c-cli-initonly-timespan-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!