Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;宏不';不能代替?_C++_Macros - Fatal编程技术网

C++ C++;宏不';不能代替?

C++ C++;宏不';不能代替?,c++,macros,C++,Macros,这是代码 #include <iostream> using namespace std; #define gao falsegao #define fun(entity) \ void fun_##entity() \ { \ std::cout << #entity << std::endl; \ } fun(gao); int main() { fun_gao(

这是代码

#include <iostream>
using namespace std;

#define gao falsegao
#define fun(entity) \
    void fun_##entity() \
    {                     \
        std::cout << #entity << std::endl; \
    }

fun(gao);

int main()
{
    fun_gao();
    return 0;
}
#包括
使用名称空间std;
#定义“高”“假高”
#定义乐趣(实体)\
void fun###entity()\
{                     \

std::cout您需要一个两级
fun

#define fun_(entity) \
    void fun_##entity() \
    {                     \
        std::cout << #entity << std::endl; \
    }

#define fun(entity) fun_(entity) 
#定义乐趣(实体)\
void fun###entity()\
{                     \
std::cout否,
##
运算符的优先级高于参数替换。习惯上,它被包装在宏中:

#define CAT_LITERAL( A, B ) A ## B
#define CAT( A, B ) CAT_LITERAL( A, B )
这同样适用于
#
操作符

#define STR_LITERAL( LIT ) # LIT
#define STR( PARAM ) STR_LITERAL( PARAM )
因此,您的宏被定义为:

#define fun(entity) \
    void CAT( fun_, entity ) () \
    {                     \
        std::cout << STR( entity ) << std::endl; \
    }
#定义乐趣(实体)\
无效猫(乐趣、实体)()\
{                     \
标准::cout