Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a double hash (##) in a macro mean?

Tags:

c++

c

In the below code, what does the ## do?

 #define MAKE_TYPE(myname) \
 typedef int myname ## Id; \
like image 982
Venkata Avatar asked Nov 19 '10 12:11

Venkata


People also ask

What is meant by double hashing?

Double hashing is a computer programming technique used in conjunction with open addressing in hash tables to resolve hash collisions, by using a secondary hash of the key as an offset when a collision occurs. Double hashing with open addressing is a classical data structure on a table .

Why is double hashing used?

Why use double hashing? Double hashing is useful if an application requires a smaller hash table since it effectively finds a free slot. Although the computational cost may be high, double hashing can find the next free slot faster than the linear probing approach.

Is double hashing more secure?

Unlike existing hash-based ciphers, the proposed scheme uses double hashing instead of a single hash function. With double hashing, the proposed scheme totally eliminates the threat of known cryptanalysis attacks and provides a highly secure stream ciphering scheme by its new design.

Whats the purpose of a hash?

Hashing is a cryptographic process that can be used to validate the authenticity and integrity of various types of input. It is widely used in authentication systems to avoid storing plaintext passwords in databases, but is also used to validate files, documents and other types of data.


2 Answers

The ## in a macro is concatenation. Here, MAKE_TYPE(test) will expand to : typedef int testId.

From 16.3.3 (The ## operator) :

For both object-like and function-like macro invocations, before the replacement list is reexamined for more macro names to replace, each instance of a ## preprocessing token in the replacement list (not from an argument) is deleted and the preceding preprocessing token is concatenated with the following preprocessing token

like image 173
icecrime Avatar answered Oct 02 '22 17:10

icecrime


icecrime is correct, but something important to point out in the definition is that the tokens need to be valid preprocessing tokens. Examples:

#define CONCAT(a,b) a ## b
CONCAT(ClassyClass, <int>); // bad, <int> is not a valid preprocessing token
CONCAT(Symbol, __LINE__); // valid as both are valid tokens
like image 43
tyree731 Avatar answered Oct 02 '22 18:10

tyree731