tensorflow中decltype使用一例分析
来源:互联网 发布:淘宝咸鱼pc搜索工具 编辑:程序博客网 时间:2024/05/17 06:26
最近一直在看tensorflow的代码,从一开始我知道其C++代码采用C++11标准。虽然对标准还算了解,但是一直用的不多。今天特分享一例tensorflow中如何使用decltype来让编译器自动推导使用的模板函数。代码如下:
/*编译器:vs2015code for test decltype*///从\tensorflow\core\util\example_proto_fast_parsing.cc摘取template <typename A>auto EnableAliasing(A* a) -> decltype(a->EnableAliasing(true), void()) { a->EnableAliasing(true); printf("Call EnableAliasing(A* a)\n");//笔者添加}template <typename A>void EnableAliasing(A&& a) { printf("Call EnableAliasing(A&& a)\n");//笔者添加}/*test_A1 和 test_A2 为笔者自定义*/class test_A1 {public: explicit test_A1() {} const char* EnableAliasing(bool enable) { const char* ptr = "Enable\n"; printf(ptr); return ptr; }//test_A1 中定义了EnableAliasing成员函数};class test_A2 {public: explicit test_A2() {}//test_A2 中没有定义EnableAliasing成员函数};int main(){ test_A1 test_a1; test_A2 test_a2; EnableAliasing(&test_a1);//调用EnableAliasing(A* a) EnableAliasing(&test_a2);//调用EnableAliasing(A&& a) test_A1 test_a3; test_A2 test_a4; EnableAliasing(test_a3);//调用EnableAliasing(A&& a) EnableAliasing(test_a4);//调用EnableAliasing(A&& a) system("pause"); return 0;}
分析
decltype:编译时进行类型推导,获取表达式的类型,不会对表达式进行求值。
上面整段代码就用到了一个decltype,用它来获取函数返回值类型。
”auto 函数名 ->decltype(表达式)“,让编译器推导函数返回值是一种经典用法。
template <typename A>auto EnableAliasing(A* a) -> decltype(a->EnableAliasing(true), void()) { //a->EnableAliasing(true);//笔者注释 printf("Call EnableAliasing(A* a)\n");//笔者添加}
定义了一个模板函数EnableAliasing(A* a)
在第一眼看到decltype(a->EnableAliasing(true), void()),我第一反应是这句话好像多余啊,这个返回类型明确为void啊!。其应该完全等价于
template <typename A>void EnableAliasing(A* a){ a->EnableAliasing(true); printf("Call EnableAliasing(A* a)\n");//笔者添加}
但是转念一想,这么写一定有它的目的。再接着往下看,看看它的意图是什么。
下面又定义了一个模板函数
EnableAliasing(A&& a)和上面的函数名一样,除了输入参数变成了右值引用。
该输入参数可以传入 &A (类的地址),也可传入A(类的对象)。
既然两个模板函数都能传入类的地址,那么编译器如何来区分的?到这大家应该猜到了就是这句话
auto EnableAliasing(A* a) -> decltype(a->EnableAliasing(true), void())
之所以这么写是告诉编译器,在编译时去匹配定义有EnableAliasing函数接口的类。如果类定义了该接口就调用该模板函数
否则调用EnableAliasing(A&& a)。
到此,不由感叹,自己只是知道标准,用的太少了。
结论
上面例子程序运行可以看出test_A1 实现了EnableAliasing接口,test_A2 没有,编译器能自动推导要调用的模板函数。
运行结果
- tensorflow中decltype使用一例分析
- C++11中decltype的使用
- tensorflow 实践(一)使用神经网络做中文情感分析
- decltype的使用
- decltype
- decltype
- decltype
- R语言中使用tensorflow
- 在tensorflow中使用CNN
- tensorflow中tfrecords使用介绍
- tensorflow 中softmax_cross_entropy_with_logits的使用
- QWheelEvent 使用分析一例
- TensorFlow学习笔记(一):快速安装与使用TensorFlow
- TensorFlow学习笔记(一补):使用Anaconda安装TensorFlow
- TensorFlow学习笔记(一):快速安装与使用TensorFlow
- Tensorflow学习(一)--使用TensorFlow实现线性回归
- tensorflow学习笔记(一)——tensorflow基本使用
- TensorFlow中global_step的简单分析
- 日期操作
- Neural Networks for Applied Sciences and Engineering--Chapter 3
- 闰年 (sdut oj)
- Day 09 学生管理(增删改查)
- 防止代码变质的思考与方法
- tensorflow中decltype使用一例分析
- Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系
- Amethyste 4.32最新安装
- 漫谈重构
- 嵌入式Linux-进程管理基础
- Cocos2d-x 3.x 图形学渲染系列二十二
- 使用工具或者IDE迅速构建一个Spring Boot应用
- Jquery 获取 radio选中值
- curl: (7) Failed to connect to raw.githubusercontent.com port 443: Operation timed out