The name c idiom is `static void * thing = & thing;`

Code:

static const void *const uniquePtr = &uniquePtr; 

... will provide a unique unique void pointer in the compilation unit. This is convenient for creating a unique descriptor or API name that would like to name void* in this way.

Examples of using:

It would be wise to wrap this template in a macro to avoid making a mistake with it, and encapsulate the idea so that it documents it.

But this leads to the question: is there a name for this idiom that can be used to refer to a macro ?:

 #define DECLARE_VOID_THING(name) static const void *const name = &name DECLARE_VOID_THING(aHandle); DECLARE_VOID_THING(anotherHandle); 

Any thoughts?

+6
source share
3 answers

As mentioned above, there is (very likely) no standard language, so you can choose whatever you think is best. Perhaps something like UNIQUE_VOID_POINTER or some such.

+1
source

I would call it self reference , it is not much different from:

 struct self { struct self *moi; } me = { &me}; 

This has the added benefit that you do not need to cast the void* pointer to use it. (for example: assert (me.moi == &me); )

+1
source

A programming idiom is a means of expressing a repeating construct in one or more programming languages.

Is this a construction that is repeated often enough to justify its "own"? Does he have the right to be an idiom by definition?

Although the question is marked as C , it seems that this is just an Objective-C thing in several APIs, and neither the language nor the individual APIs are exemplary. It serves the simple purpose of providing a unique arbitrary value, a form of lazy enumeration when the key value really is not important to you. Should there be an idiom?

I think not, and this is not my personal preference, but simple logic. This is a rarity and insignificant, mainly this value in memory, which turns out to be equal to its address in memory. It deserves to have its own name as an integer in an array with a value equal to its index in the array.

In addition, if he does not have a standardized name, this question automatically turns into “opinion-based”, which contradicts SO recommendations, in addition to being not primarily related to the programming problem. This is not a "specific programming problem", it is not a "practical, answering problem unique to software development", since the absence of a proper name is in no way problematic for its use, this is a topic for discussion in a discussion for programmers. It takes place here on SO in the same way as the question “ How do I name my variable? ” And I don’t mean the coding agreement, in the final analysis, what it is about, how to name the macro .. . and almost everything that is at least vaguely connected with its intentions to use will surpass DECLARE_VOID_THING , for example say a UNIQUE_HANDLE , and there is no need for it to be or make an idiom to solve this "problem", and finally here no idiom.

0
source

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


All Articles