• std::dclval 使用教程


    std::declval (c++11 only)

    函数模板

    template<typename T>
    typename add_rvalue_reference<T>::type declval() noexcept;

    功能描述:

     返回一个类型的右值引用,不管是否有没有默认构造函数或该类型不可以创建对象。(可以用于抽象基类);

    参数:

    返回值:

    类型T的右值引用

    1. 示例

     1 // declval example
     2 #include <utility>      // std::declval
     3 #include <iostream>     // std::cout
     4  
     5 struct A {              // abstract class
     6   virtual int value() = 0;
     7 };
     8  
     9 class B : public A {    // class with specific constructor
    10   int val_;
    11 public:
    12   B(int i,int j):val_(i*j){}
    13   int value() {return val_;}
    14 };
    15  
    16 int main() {
    17   decltype(std::declval<A>().value()) a;  // int a
    18   decltype(std::declval<B>().value()) b;  // int b
    19   decltype(B(0,0).value()) c;   // same as above (known constructor)
    20   a = b = B(10,2).value();
    21   std::cout << a << '
    ';
    22   return 0;
    23 }

    2. 示例:

     1 #include <utility>
     2 #include <iostream>
     3 
     4 struct Default { int foo() const { return 1; } };
     5 
     6 struct NonDefault
     7 {
     8     NonDefault(const NonDefault&) { }
     9     int foo() const { return 1; }
    10 };
    11 
    12 int main()
    13 {
    14     decltype(Default().foo()) n1 = 1;                   // type of n1 is int
    15 //  decltype(NonDefault().foo()) n2 = n1;               // error: no default constructor
    16     decltype(std::declval<NonDefault>().foo()) n2 = n1; // type of n2 is int
    17     std::cout<< "n1 = " << n1 << '
    '
    18     std::cout<< "n2 = " << n2 << '
    ';
    19 }
  • 相关阅读:
    伪静态规则写法RewriteRule-htaccess详细语法使用
    事务的操作
    layer 模版使用
    追加下拉框的自动生成
    PHP自动生成前端的表单框架
    MySQL高效获取记录总数
    引用JS表单验证大全 以后方便查看用
    关于left join连接查询 两张表里有同名字段的问题
    详解clientHeight、offsetHeight、scrollHeight
    php防止SQL注入详解及防范
  • 原文地址:https://www.cnblogs.com/sunbines/p/15260113.html
Copyright © 2020-2023  润新知