error C4592: symbol will be dynamically initialized. VS2015.1 static const std::vector field

后端 未结 4 1191
有刺的猬
有刺的猬 2021-02-13 00:17

after update VS2015.1 next code no longer compiles:

class testClass
{
static const std::vector test;
};

initialization



        
相关标签:
4条回答
  • 2021-02-13 00:31

    in my case I solved this by usage of std::array, and it works for me, but in general case, I suppose that warning disabling is solution

    #pragma warning( disable : 4592)
    
    0 讨论(0)
  • 2021-02-13 00:40

    Unfortunately, I don't have enough "reputation" to comment, so I'll add my comments here.

    @Peter Ruderman: complete support for constant initialization is on the list. I can't say yet whether it'll make it into Update 2 but we'll do our best. You're getting a warning for a type without constexpr constructors because of the bug which detected a different constexpr constructor in the call-tree. This usually comes from std::initializer_list<>.

    @Peter Ruderman: Also, a slight correction: for static lifetime objects initialized with a constexpr constructor call, static initialization is required only if the initializer is a constant-initializer, i.e., consists only of constant expressions. Update1 does this for but not for types containing virtual functions.

    Also: in the OP's code, no static initialization is required, though I suppose a compiler can do it for quality-of-implementation reasons. This is because 'const' was used rather than 'constexpr', the latter requiring static initialization.

    To reiterate: disabling this warning is perfectly reasonable (and safe).

    0 讨论(0)
  • 2021-02-13 00:40

    I believe that the warning is triggered by new logic for detecting when the compiler generates dynamic initializers for static objects with constexpr constructors. It doesn't make sense to raise it in this particular case because std::vector has no constexpr constructors. So that's bug number 1.

    Bug number 2 is that static objects with constexpr constructors still get dynamic initializers. This was supposed to be fixed in update 1 but apparently Microsoft was unable to do it.

    When you initialize a type with a constexpr constructor at module level, the compiler should be able to run the constructor itself and embed the fully constructed object directly in your image file. This doesn't currently work in VC, however. Instead it will create a dynamic initializer and initialize the object at runtime when your program first starts.

    The warning is trying to make this problem visible. It's safe to ignore in most cases, and it's certainly safe to ignore in this specific case.

    0 讨论(0)
  • 2021-02-13 00:52

    VC++ compiler dev here.

    Peter Ruderman's answer is almost correct. The warning was really meant for constexpr objects where a constexpr constructor call is involved in the initialization. In Update1, we can now statically initialize literal types and some non-literal types with constexpr constructors but not all. Specifically, having virtual member functions will prevent static initialization even though the type has a constexpr constructor and is supplied with a constant initializer. The warning was meant to diagnose such cases. Unfortunately, there was a bug that caused it to fire when the types of the expressions in the initializer list had constexpr constructors (in the OP's example std::vector doesn't, but std::initializer_list does). This warning can be safely ignored since the compiler is not doing anything different from before.

    We've fixed the bug but unfortunately it was found too late to include in Update 1. In any case, the need for this warning should go away when we do a more complete implementation of constant initialization (3.6.2).

    Tanveer Gani Visual C++ Team.

    0 讨论(0)
提交回复
热议问题