mac
mac

Reputation: 3

Can you not call a preprocessor macro from a specific namespace the same way you call a function from a specific namespace?

Ok so I am working on some embedded code to go on a msp430 launch pad, I was trying to toggle a pin high and I ran into an issue when I began trying to use one of the macros in the header file which was also in a specific namespace in the main.cpp file. I assumed I could use the same convention as say to call a function in that namespace for call a macro? The code would not compile until I declared using namespace, instead of msp430:: like I wanted since I have 3 different namespaces in the header file. is there not a way to do this without declaring the using namespace? I can do it with variables from a namespace as well so I don't see what I could not with a simple macro def.

expected unqualified-id before token

//section from the header file
#define P4DIR_m  *((unsigned int volatile*)0x0224u)
#define P8OUT_m  *((unsigned int volatile*)0x0262u)
#define P8DIR_m  *((unsigned int volatile*)0x0264u)
#define P1DIR_m  *((unsigned int volatile*)0x0204u)
#define LEDOUT_m *((unsigned int volatile*)0x0202u) //this line gives the error

 
//----------main.cpp

//code that called the macro orginally
msp430::LEDOUT_m |= 0x01; //this failed 

//new code
using namespace msp430;

LEDOUT_m |= 0x01; // this worked but I don't want to do this as I have other namespaces

ide snapshot with code

Upvotes: 0

Views: 192

Answers (1)

Ben
Ben

Reputation: 9703

No, macros don’t know about namespaces. Try to avoid macros whenever possible. Here you can probably do something like

namespace msp430 {
// inline requires C++17, allowing you to put this as is in the header:
inline auto& LEDOUT_m = *reinterpret_cast<unsigned int volatile*>(0x0202u);
}

// A trick I saw in a talk by Hana Dusíková that she uses for debugging her CTRE library: https://github.com/hanickadot/compile-time-regular-expressions
template <typename T> struct report_type_name;

int main() {
    msp430::LEDOUT_m |= 0x01; // Can access namespace-qualified
    namespace NS =  msp430;
    NS::LEDOUT_m = 0x0; // Can use ns aliases 'cause why not.
    using namespace msp430;
    LEDOUT_m |= 0x02; // Can use using namespace too.
    // Uncomment the next line to check that 
    // the type really is volatile unsigned int&:
    // report_type_name<decltype(LEDOUT_m)>{};
}

https://godbolt.org/z/1j7e3f

Upvotes: 1

Related Questions