Using define directive to get the namespace name

Daros7

Having such a simple C++ code:

#include <iostream>

#define NS NS_Static
#define MAKESTRING(x) #x

namespace NS {
  int global_x = 8;
  void print_global_x() {
    std::cout << MAKESTRING(NS) << "::global_x: " << global_x << std::endl;
  }
}

I'h expected to get on output NS_Static::global_x: 8 BUT getting just NS::global_x: 8. I've asked a similar question earlier and the user Eljay provided me an answer to use such an extra code:

#define NS Static
#define MAKESTRING_HELPER(x) #x
#define MAKESTRING(x) MAKESTRING_HELPER(x)

It finally works but can someone please explain my why my original approach doesn't work?

carcinogenic game

From https://gcc.gnu.org/onlinedocs/cpp/Argument-Prescan.html#Argument-Prescan

Macro arguments are completely macro-expanded before they are substituted into a macro body, unless they are stringized or pasted with other tokens.

In your original function:

#define NS NS_Static
#define MAKESTRING(x) #x

The marco argument is stringized, so NS will not be replaced by NS_Static.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

The type or namespace name 'Places' could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name 'BundleCollection' could not be found (are you missing a using directive or an assembly reference?)

using directive and function overloading in a namespace

The type or namespace name 'PrintDocument' could not be found (are you missing a using directive or an assembly reference?)

Blazor the type or namespace name 'App' could not be found (are you missing a using directive or an assembly reference?)

Error CS0246 The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?)

AzureDevOps Error The type or namespace name 'MyNamespace' could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?) fixed

c# Error 1 The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?)

Getting error The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

namespace name 'p' could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name 'nullable' could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name Edm could not be found are you missing a using directive or an assembly reference

The type or namespace name 'dbConnection' could not be found (are you missing a using directive or an assembly reference?)

Understanding namespace using directive

The type or namespace name 'FacebookSessionClient' could not be found (are you missing a using directive or an assembly reference?)

Error 1 The type or namespace name * could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name 'MapInfo' could not be found (are you missing a using directive or an assembly reference?)

Error: The type or namespace name 'SqlCe' could not be found (are you missing a using directive or an assembly reference?)

Get angular directive by name

The type or namespace name 'MvvmValidation' could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name 'IClientValidatable' could not be found (are you missing a using directive or an assembly reference?)

Unity 2019 error The type or namespace name 'NotSupportedException' could not be found (are you missing a using directive or an assembly reference?)

How to resolve "The type or namespace name 'Didstopia' could not be found (are you missing a using directive or an assembly reference?)"

The type or namespace name 'NativeShare' could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name 'Vectore3' could not be found (are you missing a using directive or an assembly reference?)

error CS0246: The type or namespace name 'LoadSceneMode' could not be found (are you missing a using directive or an assembly reference?)

Error CS0246 The type or namespace name 'Androidx' could not be found (are you missing a using directive or an assembly reference?)

CsvHelper The type or namespace name 'Configuration' could not be found (are you missing a using directive or an assembly reference?)