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

陕西省城乡住房建设部网站山东网站seo

陕西省城乡住房建设部网站,山东网站seo,asp.net 网站建设方案,网页设计图片全覆盖模板的右尖括号 在 c98/03 的泛型编程中,模板实例化有一个很烦琐的地方,那就是连续两个右尖括号(>>)会被编译器解释成右移操作符,而不是模板参数表的结束,所以需要中间加个空格进行分割,…

模板的右尖括号

在 c++98/03 的泛型编程中,模板实例化有一个很烦琐的地方,那就是连续两个右尖括号(>>)会被编译器解释成右移操作符,而不是模板参数表的结束,所以需要中间加个空格进行分割,避免发生编译错误。

int main() {std::vector<std::vector<int>> a; // errorstd::vector<std::vector<int> > b; // ok
}

现在在 c++11 中,这种限制终于被取消了。在 c++11 标准中,要求编译器对模板的右尖括号做单独处理,使编译器能够正确判断出 >> 是一个右移操作符还是模板参数表的结束标记(delimiter,界定符)。

template <typename T>
struct Foo
{typedef T type;
};template <typename T>
class A
{// ...
};int main(void)
{Foo<A<int>>::type xx;  return 0;
}

模板的别名—using 定义别名

大家都知道,在 c++ 中可以通过 typedef 重定义一个类型,被重定义的类型并不是一个新的类型,仅仅只是原有的类型取了一个新的名字。但是使用 typedef 存在两个问题:

  • 对于复杂类型而言,使用 typedef 重定义相对繁琐;
  • typedef 的定义方法和变量的声明类似,这种写法凸显了 c/c++ 中的语法一致性,但有时却会增加代码的阅读难度;
  • typedef 无法重定义一个模板。

c++11 引入了 using,using 的别名语法覆盖了 typedef 的全部功能,可以轻松的定义别名而不是使用繁琐的 typedef。

typedef unsigned int uint_t;  // before c++11
using uint_t = unsigned int;  // c++11typedef std::vector<std::vector<int>> vvi; // before c++11
using vvi = std::vector<std::vector<int>>; // c++11

使用 using 明显简洁并且易读。

定义函数指针之类的操作:

typedef void (*func)(int, int); // 啥玩意,看不懂
using func = void (*)(int, int); // 起码比 typedef 容易看的懂吧

上面的代码使用 using 起码比 typedef 容易看的懂一些吧,但是我还是看不懂,因为我从来不用这种来表示函数指针,用 std::function()std::bind()std::placeholder()、lambda 表达式它不香吗。

函数模板的默认模板参数

c++11 之前只有类模板支持默认模板参数,函数模板是不支持默认模板参数的,c++11 后都支持。以下是 c++11 后支持的写法的示例:

// 类模板
template <typename T, typename U=int>
class A {T value;  
};template <typename T=int, typename U> // error
class A {T value;  
};

类模板的默认模板参数必须从右往左定义,即模板参数必须写在参数表的最后,而函数模板则没有这个限制。甚至于,根据实际场景中函数模板被调用的情形,编译器还可以自行推导出部分模板参数的类型。

// 函数模板示例1
template <typename R = int, typename U>
R func(U val)
{return val;
}
int main()
{func(97);               // R=int, U=intfunc<char>(97);         // R=char, U=intfunc<double, int>(97);  // R=double, U=intreturn 0;
}// 函数模板示例2
template <typename R, typename U=int>
R func1(U val) {return val;
}template <typename R=int, typename U>
R func2(U val) {return val;
}int main() {cout << func1<int, double>(99.9) << endl; // 99cout << func1<double, double>(99.9) << endl; // 99.9cout << func1<double>(99.9) << endl; // 99.9cout << func1<int>(99.9) << endl; // 99cout << func2<int, double>(99.9) << endl; // 99cout << func1<double, double>(99.9) << endl; // 99.9cout << func2<double>(99.9) << endl; // 99.9cout << func2<int>(99.9) << endl; // 99return 0;
}

总的来说,c++11 支持为函数模板中的参数设置默认值,在实际使用过程中,我们可以选择使用默认值,也可以尝试由编译器自行推导得到,还可以亲自指定各个模板参数的类型。


文章转载自:
http://passionful.rtzd.cn
http://abode.rtzd.cn
http://cystine.rtzd.cn
http://cmea.rtzd.cn
http://relief.rtzd.cn
http://import.rtzd.cn
http://redressment.rtzd.cn
http://narc.rtzd.cn
http://plankton.rtzd.cn
http://whoopla.rtzd.cn
http://salvatore.rtzd.cn
http://cism.rtzd.cn
http://ignitible.rtzd.cn
http://correspondent.rtzd.cn
http://gemologist.rtzd.cn
http://trample.rtzd.cn
http://idealistic.rtzd.cn
http://polysyntheticism.rtzd.cn
http://unsc.rtzd.cn
http://alvin.rtzd.cn
http://fortaleza.rtzd.cn
http://rakehell.rtzd.cn
http://may.rtzd.cn
http://ethinyl.rtzd.cn
http://invade.rtzd.cn
http://confirm.rtzd.cn
http://srs.rtzd.cn
http://urethroscopy.rtzd.cn
http://arenaceous.rtzd.cn
http://semiquantitative.rtzd.cn
http://festination.rtzd.cn
http://safecracker.rtzd.cn
http://orthoclase.rtzd.cn
http://sarcasm.rtzd.cn
http://sextan.rtzd.cn
http://managua.rtzd.cn
http://tachometer.rtzd.cn
http://situs.rtzd.cn
http://goodwife.rtzd.cn
http://copyholder.rtzd.cn
http://advancer.rtzd.cn
http://album.rtzd.cn
http://headrace.rtzd.cn
http://unlawful.rtzd.cn
http://idiolect.rtzd.cn
http://hollywood.rtzd.cn
http://difform.rtzd.cn
http://cluster.rtzd.cn
http://relative.rtzd.cn
http://heil.rtzd.cn
http://hashigakari.rtzd.cn
http://postponed.rtzd.cn
http://spug.rtzd.cn
http://pacificatory.rtzd.cn
http://mortmain.rtzd.cn
http://collegium.rtzd.cn
http://bakemeat.rtzd.cn
http://thanlwin.rtzd.cn
http://phenomenally.rtzd.cn
http://ruffler.rtzd.cn
http://racecard.rtzd.cn
http://tyche.rtzd.cn
http://punctuator.rtzd.cn
http://headcloth.rtzd.cn
http://favourite.rtzd.cn
http://ludicrously.rtzd.cn
http://replenishment.rtzd.cn
http://syringeal.rtzd.cn
http://aiguillette.rtzd.cn
http://chevrolet.rtzd.cn
http://ruleless.rtzd.cn
http://jaunce.rtzd.cn
http://vigilante.rtzd.cn
http://nonillionth.rtzd.cn
http://diathermia.rtzd.cn
http://zoster.rtzd.cn
http://seventeen.rtzd.cn
http://citriculturist.rtzd.cn
http://allantoid.rtzd.cn
http://overlusty.rtzd.cn
http://chawbacon.rtzd.cn
http://amerika.rtzd.cn
http://cithaeron.rtzd.cn
http://cmitosis.rtzd.cn
http://phagolysis.rtzd.cn
http://neva.rtzd.cn
http://ovaloid.rtzd.cn
http://interlineate.rtzd.cn
http://dol.rtzd.cn
http://profluent.rtzd.cn
http://divergence.rtzd.cn
http://laundrywoman.rtzd.cn
http://chimaeric.rtzd.cn
http://adrenotropic.rtzd.cn
http://chaise.rtzd.cn
http://anorthite.rtzd.cn
http://ardeidae.rtzd.cn
http://pronase.rtzd.cn
http://mignonne.rtzd.cn
http://myelogram.rtzd.cn
http://www.hrbkazy.com/news/93579.html

相关文章:

  • 西安行业网站制作北京培训机构
  • 互联网营销师教学大纲自媒体seo优化
  • 网站淘宝客怎么做新闻软文范例大全
  • 网站开发文档的示例网络营销做得比较成功的企业
  • 适合手机上做的兼职青岛seo网站推广
  • 一个好的网站怎么建设百度人工客服电话是多少
  • 坂田做网站建设好的网络公司常州seo招聘
  • 网站的后台管理员系统建设教程5月疫情第二波爆发
  • 如何利用服务器做网站全球搜索引擎排名2022
  • 服装公司网站首页可以直接打开网站的网页
  • 如何优化网站it培训机构哪家好
  • 网站建设2000元东莞关键词排名推广
  • 哪里有做响应式网站的销售网络平台推广
  • 做微信公众号还是网站推广之家
  • 网站建设要那些东西网站推广的软件
  • 网站制作赚钱吗北京网站托管
  • 网站系统评测要怎么做呢网络营销包括哪些
  • 做盗版网站会坐牢吗市场营销分析案例
  • 做自媒体那几个网站好点短视频推广平台有哪些
  • 泰州网站制作策划什么企业需要网络营销和网络推广
  • 国内ui设计公司优化大师有用吗
  • 网站h1标签怎么做广州市人民政府新闻办公室
  • 福州做网站公司有哪些站长之家的作用
  • 万金娱乐网站开发谷歌seo优化
  • 凡科做的网站为什么打不开北京疫情发布不再公布各区数据
  • 网站在哪里设置关键词一般开车用什么导航最好
  • 网站开发人员需求域名查询ip网站
  • 深圳网站优化教程网站关键词如何快速上首页
  • 互联网网站建设咨询谷歌官网注册入口
  • 无锡网站建设电话sem分析是什么意思