Skip to content

difference between "using" and "#define" #10

Answered by hsaturn
hpssjellis asked this question in Q&A
Discussion options

You must be logged in to vote

Hi Jeremy (Lost my answer GRRRR)

#define is just a preprocessing string replacement (not seen by the compiler)

using is a type alias interpreted by the compiler (not the preprocessor)

An example where using cannot be used : https://onlinegdb.com/MzKsCVN5p

An example where #define produces bad things https://www.onlinegdb.com/Mo5xRNL0G

#define replaces the string in ANY context (except inside strings), while USING uses the alias only within type context.

A last example showing why using is better when used for types https://onlinegdb.com/drGp8aWKc

using TcpClient = WiFiClient; // ==> Tells the compiler to use WifiClient as real class when TcpClient is encountered.

using namespace mbed; Is …

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@hpssjellis
Comment options

Answer selected by hsaturn
Comment options

You must be logged in to vote
1 reply
@hpssjellis
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants