不能像从特定命名空间调用函数那样从特定命名空间调用预处理器宏吗?
Can you not call a preprocessor macro from a specific namespace the same way you call a function from a specific namespace?
好的,所以我正在研究一些嵌入式代码以在 msp430 发射台上运行,我试图将一个引脚切换为高电平,但当我开始尝试使用其中一个宏时,我 运行 遇到了问题在头文件中,该文件也在 main.cpp 文件的特定命名空间中。我假设我可以使用与 say 相同的约定来调用该命名空间中的函数来调用宏?在我声明使用命名空间之前,代码不会编译,而不是像我想要的那样使用 msp430::,因为我在头文件中有 3 个不同的命名空间。有没有办法在不声明 using 命名空间的情况下做到这一点?我也可以使用名称空间中的变量来完成它,所以我看不到我用简单的宏定义做不到的事情。
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
不,宏不知道命名空间。尽可能避免使用宏。在这里你可以做类似
的事情
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)>{};
}
好的,所以我正在研究一些嵌入式代码以在 msp430 发射台上运行,我试图将一个引脚切换为高电平,但当我开始尝试使用其中一个宏时,我 运行 遇到了问题在头文件中,该文件也在 main.cpp 文件的特定命名空间中。我假设我可以使用与 say 相同的约定来调用该命名空间中的函数来调用宏?在我声明使用命名空间之前,代码不会编译,而不是像我想要的那样使用 msp430::,因为我在头文件中有 3 个不同的命名空间。有没有办法在不声明 using 命名空间的情况下做到这一点?我也可以使用名称空间中的变量来完成它,所以我看不到我用简单的宏定义做不到的事情。
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
不,宏不知道命名空间。尽可能避免使用宏。在这里你可以做类似
的事情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)>{};
}