TR1中is_base_of的实现

本文介绍了C++ TR1中is_base_of模板的实现原理,探讨了一个可能不常见的匹配顺序问题。通过分析源代码,解释了如何通过模板特性和隐式转换判断一个类型是否为另一个类型的基类。文中还提供了一个简化版的is_base_of实现,并以实例展示了其工作过程,强调了在匹配函数选择时的关键细节。
 

 今天不知何故,一个意外神游到is_base_of的原代码处,发现了一个以前从没碰上的匹配顺序的情况,貌似C++的书里都没提到过这种情况,也可能是自己看书不认真,哈哈。Whatever,确实是一个很tricky的东西,就在这里小记一下吧。

 

is_base_of以两个类型作为模板参数,然后用模板里定义的value来说明前一个是否后一个的基类,当然,如果这两个的类型完全相同,那么value的值也是真。是非常典型的模板trait。下面代码是自己写的一个缩简版,仅供说明问题:

 

  1. #include <iostream>
  2. using namespace std;
  3. template <typename T1, typename T2>
  4. struct is_same
  5. {
  6.     static const bool v
user@YBXzuiniubi:~/Livox-SDK/build$ cmake .. make sudo make install -- main project dir: /home/user/Livox-SDK -- Configuring done -- Generating done -- Build files have been written to: /home/user/Livox-SDK/build Consolidate compiler generated dependencies of target livox_sdk_static [ 2%] Building CXX object sdk_core/CMakeFiles/livox_sdk_static.dir/src/base/thread_base.cpp.o In file included from /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:25: /home/user/Livox-SDK/sdk_core/src/base/thread_base.h:46:8: error: ‘shared_ptr’ in namespace ‘std’ does not name a template type 46 | std::shared_ptr<std::thread> thread_; | ^~~~~~~~~~ /home/user/Livox-SDK/sdk_core/src/base/thread_base.h:30:1: note: ‘std::shared_ptris defined in header ‘<memory>’; did you forget to ‘#include <memory>’? 29 | #include "noncopyable.h" +++ |+#include <memory> 30 | /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp: In member function ‘virtual bool livox::ThreadBase::Start()’: /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:34:3: error: ‘thread_’ was not declared in this scope; did you mean ‘pthread_t’? 34 | thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this); | ^~~~~~~ | pthread_t /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:34:18: error: ‘make_shared’ is not a member of ‘std’ 34 | thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this); | ^~~~~~~~~~~ /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:26:1: note: ‘std::make_shared’ is defined in header ‘<memory>’; did you forget to ‘#include <memory>’? 25 | #include "thread_base.h" +++ |+#include <memory> 26 | #include <thread> /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:34:41: error: expected primary-expression before ‘>’ token 34 | thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this); | ^ /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:34:68: error: left operand of comma operator has no effect [-Werror=unused-value] 34 | thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this); | ^~~~ /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp: In member function ‘virtual void livox::ThreadBase::Join()’: /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:41:5: error: ‘thread_’ was not declared in this scope; did you mean ‘pthread_t’? 41 | thread_->join(); | ^~~~~~~ | pthread_t At global scope: cc1plus: note: unrecognized command-line option ‘-Wno-c++11-long-long’ may have been intended to silence earlier diagnostics cc1plus: all warnings being treated as errors make[2]: *** [sdk_core/CMakeFiles/livox_sdk_static.dir/build.make:104: sdk_core/CMakeFiles/livox_sdk_static.dir/src/base/thread_base.cpp.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:242: sdk_core/CMakeFiles/livox_sdk_static.dir/all] Error 2 make: *** [Makefile:136: all] Error 2 Consolidate compiler generated dependencies of target livox_sdk_static [ 2%] Building CXX object sdk_core/CMakeFiles/livox_sdk_static.dir/src/base/thread_base.cpp.o In file included from /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:25: /home/user/Livox-SDK/sdk_core/src/base/thread_base.h:46:8: error: ‘shared_ptr’ in namespace ‘std’ does not name a template type 46 | std::shared_ptr<std::thread> thread_; | ^~~~~~~~~~ /home/user/Livox-SDK/sdk_core/src/base/thread_base.h:30:1: note: ‘std::shared_ptris defined in header ‘<memory>’; did you forget to ‘#include <memory>’? 29 | #include "noncopyable.h" +++ |+#include <memory> 30 | /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp: In member function ‘virtual bool livox::ThreadBase::Start()’: /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:34:3: error: ‘thread_’ was not declared in this scope; did you mean ‘pthread_t’? 34 | thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this); | ^~~~~~~ | pthread_t /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:34:18: error: ‘make_shared’ is not a member of ‘std’ 34 | thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this); | ^~~~~~~~~~~ /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:26:1: note: ‘std::make_shared’ is defined in header ‘<memory>’; did you forget to ‘#include <memory>’? 25 | #include "thread_base.h" +++ |+#include <memory> 26 | #include <thread> /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:34:41: error: expected primary-expression before ‘>’ token 34 | thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this); | ^ /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:34:68: error: left operand of comma operator has no effect [-Werror=unused-value] 34 | thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this); | ^~~~ /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp: In member function ‘virtual void livox::ThreadBase::Join()’: /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:41:5: error: ‘thread_’ was not declared in this scope; did you mean ‘pthread_t’? 41 | thread_->join(); | ^~~~~~~ | pthread_t At global scope: cc1plus: note: unrecognized command-line option ‘-Wno-c++11-long-long’ may have been intended to silence earlier diagnostics cc1plus: all warnings being treated as errors make[2]: *** [sdk_core/CMakeFiles/livox_sdk_static.dir/build.make:104: sdk_core/CMakeFiles/livox_sdk_static.dir/src/base/thread_base.cpp.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:242: sdk_core/CMakeFiles/livox_sdk_static.dir/all] Error 2 make: *** [Makefile:136: all] Error 2 user@YBXzuiniubi:~/Livox-SDK/build$
最新发布
09-20
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值