1

As far as I remember, the keyword static, for C++, inside a function call meant that the static variable was initialized the first time the declaration was encountered.

I have used the static keyword in an Objective-C member function and the static variable seems to be initialized immediately (with a non-compile-time constant subscriber). Note, that I am coding in a mixed file type .mm of Objective-C.

- (id) init
{
    [self initSubsciber];
    [self relayMessages];
}

- (void) initSubscriber
{
    subscriber= PTR;
}

- (void) relayMessages
{
    // Example 2-2 (mspoller.c), 0MQ book pg.43
    // Initialize poll set
    static zmq_pollitem_t items[] = {
        { subscriber, 0, ZMQ_POLLIN, 0 },
};

In the above example, since I am calling initSubscriber before relayMessages, I would expect the subscribermember variable pointer to be equal to PTR, not NULL, because the zmq_pollitem_t line hasn't been called. Yet, it is NULL.

Has the behavior of static changed in the various versions of C++? And, how is the behavior defined in Objective-C in comparison?

1

1 Answer 1

1

No, C didn't change. That what you expect is how C++ works.

C

en.cppreference.com/w/c/language/storage_duration

Storage duration

[...]

  • automatic storage duration. [...]

  • static storage duration. The storage duration is the entire execution of the program, and the value stored in the object is initialized only once, prior to main function. All objects declared static and all objects with either internal or external linkage that aren't declared _Thread_local (since C11) have this storage duration.

C++

en.cppreference.com/w/cpp/language/storage_duration

Static local variables

Variables declared at block scope with the specifier static have static storage duration but are initialized the first time control passes through their declaration (unless their initialization is zero- or constant-initialization, which can be performed before the block is first entered). On all further calls, the declaration is skipped.

Objective-C++ (.mm)

.mm is Objective-C++, so C++ should apply. But that said there is no official specification of language. So to be sure move in .cpp files and leave only some interfacing code in the .mm. For example it's not clear whether/how the thread-safe local static work (Is it possible to remove dispatch_once in Objective-C++?).

What is Objective C++?

Also, you might want to read Apple's (sadly deleted, but archived) documentation on Objective-C++.

-- doches

Clang has a common unified parser for all C language family[5][6] and it looks like the single features are activated depending on the file type/extension and language standard support[7].

Sign up to request clarification or add additional context in comments.

7 Comments

Ok, but in relation to Objective-C then? If I am using a .mm file am I compiling with a C or C++ standard? Neither?
.mm is Objective-C++, so C++. But that said there is no official specification of language. So to be sure move in .cpp files and leave only some interfacing code in the .mm
The project file says, I am using dialect: LLVM C++ std lib with C++11 support. Compiler warnings state: ISO C++11.
It all depends on the LLVM Objective-C++ implementation. But as I said no official spec. or documentation. Somehow gray zone :) stackoverflow.com/a/3684159/8918119
I think it's even before LLVM and swift era so maybe now its not C++ extension to Objective-C but the opposite to get most of the C++11 features working. Maybe someone on the Clang team knows better. clang.llvm.org/docs/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.