当前位置: 首页 > news >正文

西宁网站制作哪家好来客seo

西宁网站制作哪家好,来客seo,企查猫,济南哪里有网站公司一、decltype 关键字 1、介绍 decltype 是 C11 新增的一个用来推导表达式类型的关键字。和 auto 的功能一样,用来在编译时期进行自动类型推导。引入 decltype 是因为 auto 并不适用于所有的自动类型推导场景,在某些特殊情况下 auto 用起来很不方便&…

一、decltype 关键字

1、介绍

decltype 是 C++11 新增的一个用来推导表达式类型的关键字。和 auto 的功能一样,用来在编译时期进行自动类型推导。引入 decltype 是因为 auto 并不适用于所有的自动类型推导场景,在某些特殊情况下 auto 用起来很不方便,甚至压根无法使用。

auto varName = value;
decltype(exp) varName = value;

auto与decltype在形式上的区别,如下:

  • auto 根据 = 右边的初始值推导出变量的类型,decltype 根据 exp 表达式推导出变量的类型,跟 = 右边的 value 没有关系
  • auto 要求变量必须初始化,因为 auto 是根据变量的初始值来推导变量类型的,如果不初始化,变量的类型也就无法推导。而 decltype 不要求,因此可以写成如下形式:
decltype(exp) varName;

2、推导规则

(1)如果 exp 是一个不被括号()包围的表达式,或者是一个类成员访问表达式,或者是一个单独的变量,decltype(exp) 的类型和 exp 一致

#include<string> 
#include<iostream>
using namespace std;class A {
public:static int total;string name;int age;float scores;
}int A::total = 0;int main()
{int n = 0;const int &r = n;A a;decltype(n) x = n;           // n 为 int,x 被推导为 intdecltype(r) y = n;           // r 为 const int &,y 被推导为 const int &decltype(A::total)  z = 0;   // total 是类 A 的一个 int 类型的成员变量,z 被推导为 intdecltype(A.name) url = "www.baidu.com"; // url 为 string 类型return 0;
}

(2)如果 exp 是函数调用,则 decltype(exp) 的类型就和函数返回值的类型一致

int& func1(int, char);   // 函数返回值为 int&
int&& func2(void);       // 函数返回值为 int&&
int func3(double);       // 函数返回值为 intconst int& func4(int, int, int);  // 函数返回值为 const int&
const int&& func5(void);          // 函数返回值为 const int&&int n = 50;
decltype(func1(100,'A')) a = n; // a 的类型为 int&
decltype(func2()) b = 0;        // b 的类型为 int&&
decltype(func3(10.5)) c = 0;    // c 的类型为 intdecltype(func4(1,2,3)) x = n;    // x 的类型为 const int&
decltype(func5()) y = 0;         // y 的类型为 const int&&

(3)如果 exp 是一个左值,或被括号()包围,decltype(exp) 的类型就是 exp 的引用,假设 exp 的类型为 T,则 decltype(exp) 的类型为 T&

class A 
{
public:int x;
}int main()
{const A obj;decltype(obj.x) a = 0;   // a 的类型为 intdecltype((obj.x)) b = a; // b 的类型为 int&int n = 0, m = 0;decltype(m + n) c = 0;     // n + m 得到一个右值,c 的类型为 intdecltype(n = n + m) d = c; // n = n + m 得到一个左值,d 的类型为 int &return 0;
}

(4)类的静态成员可以使用 auto, 对于类的非静态成员无法使用 auto,如果想推导类的非静态成员的类型,只能使用 decltype

template<typename T>
class A
{
private :decltype(T.begin()) m_it;public:void func(T& container){m_it = container.begin();}
};int main()
{const vector<int> v;A<const vector<int>> obj;obj.func(v);return 0;
}

(5)使用decltype推导参数类型

可以在函数模板、类模板和 lambda 表达式中使用 decltype,从而推断类型或者声明类型。下面给出的示例代码展示了如何在 lambda 表达式中使用 decltype,推导出参数类型

#include <iostream>int main()
{int x = 42;auto f = [&](decltype(x)& val) { val += 1; };f(x);std::cout << "x: " << x << std::endl;  // x: 43return 0;
}

在这个例子中,定义了一个 lambda 表达式 f,它的参数使用了 decltype 推断出参数类型为 int&。由于在 lambda 表达式中使用的变量必须是可见的,因此在 lambda 表达式前面的捕获列表中使用了 [&],以让 lambda 表达式捕获 x。最后,调用 lambda 表达式 f,并将变量 x 作为参数传递给它,从而使 x 的值被加 1。

http://www.yidumall.com/news/11501.html

相关文章:

  • 手工艺品网站建设方案今日头条网页版入口
  • 常德网站建设公司小广告公司如何起步
  • 万户做网站很垃圾怎么制作网页教程
  • 织梦网站如何做移动端网络整合营销
  • 使用三剑客做网站五个成功品牌推广案例
  • 做静态页面的网站百度手机助手免费下载
  • 厦门电子商务网站建营销方案的几个要素
  • 武汉规划建设局网站seo技术培训广东
  • 做电影网站赚钱吗长沙关键词优化新行情报价
  • 重庆网站建设023kw搜索引擎优化策略包括
  • 做网站哪里找程序员网上营销模式
  • 收费网站有哪些网络营销师证书查询
  • 网站建设销售沈阳网站建设
  • 网站内容页做301近期网络舆情事件热点分析
  • 怎么找做网站客服的工作武汉关键词seo
  • wordpress 评论小工具泉州seo网站排名
  • 网站二维码制作爱站关键词挖掘工具
  • 寻花问柳一家专门做男人的网站优化服务平台
  • 做国际网站有用吗百度一下的网址
  • ps做网站图片水印互联网营销师培训班
  • 刺猬猫网站维护哪个平台可以随便发广告
  • 做网站建设费用说说seo论坛
  • 十大咨询公司排行榜杭州seo排名收费
  • 辽宁省建设工程信息网官网新网站入口网站seo推广计划
  • 网站app开发平台网站外包
  • 虎门网站中国十大小说网站排名
  • java做的是网站还是系统电脑培训班速成班
  • 重庆网站开发建设杭州旺道企业服务有限公司
  • 网站后台fpt百度搜索下载app
  • 在县城做团购网站网络推广培训班