"Naked function cannot contain objects that require unwinding ..." for statically initialized variables

I had a problem when using Visual Studio 15 to compile. This code illustrates this:

const char* getx() { return "foo"; }

void __declspec(naked) nf()
{
    static const char* x = getx();
}

It is not satisfied with the following error: Error C3068 'nf': a 'naked' function cannot contain objects that would require unwinding if a C++ exception occurred. I really don't understand why this fails; static objects are not automatic, and from the point of view of storage, they behave more or less like global ones and are initialized before the entry point is executed (as far as I know). If so, what kind of unleashing is this message? At this point, there is nothing on the stack, so nothing needs to be untwisted. In addition, if I delete a function call, declaring a variable is staticfine (just like calling a function without assignment, or assigning a constant value to a variable x, for example static const char* x = 0;).

Did I miss something?

+4
source share
2 answers

A noexcept , ++ 11, , .

, getx() , noexcept, .

, nf(), , , , -, "" , .

getx noexcept , , , "" .

+1

:

. .

, - , - , .

, () , , . -, , "".

+1

Source: https://habr.com/ru/post/1622398/


All Articles