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?

PLG
  • 448
  • 5
  • 21
  • 1
    No, C didn't change.That what you expect is how C++ works. http://en.cppreference.com/w/c/language/storage_duration (and fo C++ lstatic local - http://en.cppreference.com/w/cpp/language/storage_duration) – Mihayl Jan 26 '18 at 11:42

1 Answers1

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].

Mihayl
  • 3,821
  • 2
  • 13
  • 32
  • 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? – PLG Jan 26 '18 at 11:52
  • 1
    .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 – Mihayl Jan 26 '18 at 11:54
  • The project file says, I am using dialect: LLVM C++ std lib with C++11 support. Compiler warnings state: ISO C++11. – PLG Jan 26 '18 at 12:02
  • It all depends on the LLVM Objective-C++ implementation. But as I said no official spec. or documentation. Somehow gray zone :) https://stackoverflow.com/a/3684159/8918119 – Mihayl Jan 26 '18 at 12:07
  • I did read the Objective-C guide (a long time ago; still have it) and I remember it says nothing useful. – PLG Jan 26 '18 at 12:17
  • 1
    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. https://clang.llvm.org/docs/LanguageExtensions.html#objective-c-features – Mihayl Jan 26 '18 at 12:19
  • [-rewrite-objc and Objective-C in clang](https://stackoverflow.com/q/44561285/8918119) – Mihayl Jan 26 '18 at 12:28