namespace NejmSpejs{
struct Bar{};
}
namespace InnyNejmSpejs{
typedef NejmSpejs::Bar fun;
}
#define InnyNejmspejs::Foo NejmSpejs::Bar;
int main(){
InnyNejmspejs::Foo x;
return 0;
}
prog.cpp22: warning: missing whitespace after the macro name [enabled by default]
#define InnyNejmspejs::Foo NejmSpejs::Bar;
^
prog.cpp: In function ‘int main()’:
prog.cpp22: error: ‘::Foo’ has not been declared
#define InnyNejmspejs::Foo NejmSpejs::Bar;
^
prog.cpp5: note: in expansion of macro ‘InnyNejmspejs’
InnyNejmspejs::Foo x;
^
prog.cpp28: error: expected ‘;’ before ‘NejmSpejs’
#define InnyNejmspejs::Foo NejmSpejs::Bar;
^
prog.cpp5: note: in expansion of macro ‘InnyNejmspejs’
InnyNejmspejs::Foo x;
^
prog.cpp18: error: ‘::Foo’ has not been declared
InnyNejmspejs::Foo x;
^
prog.cpp24: error: expected ‘;’ before ‘x’
InnyNejmspejs::Foo x;
Podejście nr 2:
namespace NejmSpejs{
struct Bar{};
}
namespace InnyNejmSpejs{
typedef NejmSpejs::Bar fun;
#define Foo NejmSpejs::Bar;
}
int main(){
InnyNejmSpejs::Foo x;
return 0;
}
prog.cpp: In function ‘int main()’:
prog.cpp17: error: ‘InnyNejmSpejs::NejmSpejs’ has not been declared
#define Foo NejmSpejs::Bar;
^
prog.cpp20: note: in expansion of macro ‘Foo’
InnyNejmSpejs::Foo x;
^
prog.cpp24: error: ‘x’ was not declared in this scope
InnyNejmSpejs::Foo x;
^
w C++ istnieje wiele mechanizmów, które zastępują makra.
W ogóle makra są be :P POTRZEBA ich użycia przychodzi naprawdę rzadko.