C++ stander library–type traits and type utility

C++11 提供了许多的类型特征和型别实用函数。

1 declval

01122   /// declval
01123   template<typename _Tp>
01124     struct __declval_protector
01125     {
01126       static const bool __stop = false;
01127       static typename add_rvalue_reference<_Tp>::type __delegate();
01128     };
01129 
01130   template<typename _Tp>
01131     inline typename add_rvalue_reference<_Tp>::type
01132     declval() noexcept
01133     {
01134       static_assert(__declval_protector<_Tp>::__stop,
01135             "declval() must not be used!");
01136       return __declval_protector<_Tp>::__delegate();
01137     }

Utility to simplify expressions used in unevaluated operands (such as the operands of sizeof and decltype)..
返回右值引用,用于不求值的操作,使得在不需要指定构造函数的额情况下使用它的成员函数。因为他是一个无值环境所以你不能对他进行求值操作。例如:

// declval example
#include <utility>      // std::declval
#include <iostream>     // std::cout

struct A {              // abstract class
  virtual int value() = 0;
};

class B : public A {    // class with specific constructor
  int val_;
public:
  B(int i,int j):val_(i*j){}
  int value() {return val_;}
};

int main() {
  decltype(std::declval<A>().value()) a;  // int a
std::cout <<std::declval<A>().value()<< '\n'; //出错,编译不成功
decltype(std::declval<B>().value()) b; // int b 

decltype(B(0,0).value()) c; // same as above (known constructor)

std::cout<<c<<std::endl; // a = b = B(10,2).value(); std::cout << a << '\n'; //输出:20 return 0;}

2 common_type

 1788   /// common_type
 1789   template<typename... _Tp>
 1790     struct common_type;
 1791 
 1792   // Sfinae-friendly common_type implementation:
 1793 
 1794   struct __do_common_type_impl
 1795   {
 1796     template<typename _Tp, typename _Up>
 1797       static __success_type<typename decay<decltype
 1798                 (true ? std::declval<_Tp>()
 1799                  : std::declval<_Up>())>::type> _S_test(int); //转换成共同类型的核心,使用三目运算符自动转换成共同类型,这里使用的是隐式转换
 1800 
 1801     template<typename, typename>            
 1802       static __failure_type _S_test(...);  //?
 1803   };
 1804 
 1805   template<typename _Tp, typename _Up>
 1806     struct __common_type_impl
 1807     : private __do_common_type_impl
 1808     {
 1809       typedef decltype(_S_test<_Tp, _Up>(0)) type;
 1810     };
 1811 
 1812   struct __do_member_type_wrapper
 1813   {
 1814     template<typename _Tp>
 1815       static __success_type<typename _Tp::type> _S_test(int);
 1816 
 1817     template<typename>
 1818       static __failure_type _S_test(...);
 1819   };
 1820 
 1821   template<typename _Tp>
 1822     struct __member_type_wrapper
 1823     : private __do_member_type_wrapper
 1824     {
 1825       typedef decltype(_S_test<_Tp>(0)) type;
 1826     };
 1827 
 1828   template<typename _CTp, typename... _Args>
 1829     struct __expanded_common_type_wrapper
 1830     {
 1831       typedef common_type<typename _CTp::type, _Args...> type;
 1832     };
 1833 
 1834   template<typename... _Args>
 1835     struct __expanded_common_type_wrapper<__failure_type, _Args...>
 1836     { typedef __failure_type type; };
 1837 
 1838   template<typename _Tp>
 1839     struct common_type<_Tp>
 1840     { typedef typename decay<_Tp>::type type; };
 1841 
 1842   template<typename _Tp, typename _Up>
 1843     struct common_type<_Tp, _Up>
 1844     : public __common_type_impl<_Tp, _Up>::type
 1845     { };
 1846 
 1847   template<typename _Tp, typename _Up, typename... _Vp>
 1848     struct common_type<_Tp, _Up, _Vp...>
 1849     : public __expanded_common_type_wrapper<typename __member_type_wrapper<
 1850                common_type<_Tp, _Up>>::type, _Vp...>::type
 1851     { };

这段代码还是不甚理解,只知道他是一个递归定义解决可变参数的问题,真正求共同类型使用的条件运算符?:,所使用的还是隐式转换。(待跟进...)


原文链接: https://www.cnblogs.com/xuning2516/archive/2013/04/12/3018745.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++ stander library--type traits and type utility

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/84218

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午9:28
下一篇 2023年2月9日 下午9:28

相关推荐