博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++ define 定义工厂函数,使用##连接
阅读量:4210 次
发布时间:2019-05-26

本文共 1371 字,大约阅读时间需要 4 分钟。

c++ define 定义工厂函数,其中define函数名使用##连接

# include
# include
//atoi# include
//strlen# include
//uint64_t#include
#include
using namespace std;template
int DoAllreduce(T tensor, T output) { return tensor+output;}#define ALLREDUCE(torch_Tensor, THTensor) \ extern "C" int horovod_torch_allreduce_async_##torch_Tensor( \ THTensor tensor, THTensor output) { \ printf("this is all reduce!\n");\ DoAllreduce(1,1);\ return 0; \ }ALLREDUCE(torch_bbyte,int)ALLREDUCE(torch_bbyte2,int)int main(){ horovod_torch_allreduce_async_torch_bbyte(1,1); horovod_torch_allreduce_async_torch_bbyte2(1,1); return 0;}

输出:

this is a construc function!the sum of A is 3share_ptr:00871244请按任意键继续. . .

区别#与##

# 是把参数字符串化,## 是将两个参数连为一个整体。

#include 
#include
// 测试不带 # 号的效果#define NO_NUMBER_SIGN(x) x// 测试一个 # 号的效果#define SINGLE_NUMBER_SIGN(x) #x// 测试两个 # 号的效果#define DOUBLE_NUMBER_SIGN(x, y) x##yint main(){ // 测试不带 # 号的效果:hello 不加双引号报错(error C2065: “hello”: 未声明的标识符) std::cout << NO_NUMBER_SIGN("hello") << std::endl; // 测试一个 # 号的效果:world 加不加双引号都可以,因为 # 将其字符串化 std::cout << SINGLE_NUMBER_SIGN(world) << std::endl; // 测试两个 # 号的效果:连接两个对象 std::cout << DOUBLE_NUMBER_SIGN("hello", "world") << std::endl; system("pause"); return 0;}

输出:

hello world helloworld

转载地址:http://hzwmi.baihongyu.com/

你可能感兴趣的文章
hdu 3460 Ancient Printer(trie tree)
查看>>
中间数
查看>>
KMP求前缀函数(next数组)
查看>>
KMP
查看>>
poj 3863Business Center
查看>>
Android编译系统简要介绍和学习计划
查看>>
Android编译系统环境初始化过程分析
查看>>
user2eng 笔记
查看>>
DRM in Android
查看>>
ARC MRC 变换
查看>>
Swift cell的自适应高度
查看>>
【linux】.fuse_hiddenXXXX 文件是如何生成的?
查看>>
【LKM】整合多个LKM为1个
查看>>
【Windows C++】调用powershell上传指定目录下所有文件
查看>>
Java图形界面中单选按钮JRadioButton和按钮Button事件处理
查看>>
小练习 - 排序:冒泡、选择、快排
查看>>
SparkStreaming 如何保证消费Kafka的数据不丢失不重复
查看>>
Spark Shuffle及其调优
查看>>
数据仓库分层
查看>>
常见数据结构-TrieTree/线段树/TreeSet
查看>>