在调用C++ template函数时,有时候语法会存在歧义。

  • 调用函数时,使用 obj.template func<...>() 形式的语法,避免歧义;
  • 调用类型时,使用嵌入子类型时,使用 a_type::typename sub_type形式的语法,避免歧义。

typename关键字

例如template代码中有定义:T::iterator,它只有等到模板实例化时才会知道是哪种类型,更不用说内部的iterator。T::iterator实际上可以是以下三种中的任何一种类型:

  • 静态数据成员
  • 静态成员函数
  • 嵌套类型
template <class T>
void foo() {
typename T::iterator * iter;
// ...
}

这样编译器就可以确定T::iterator是一个类型,消除了前面提到的歧义。

template关键字

例如有如下代码,会引起歧义:

namespace boost { int function = 0; }
int main() {
int f = 0;
boost::function< int() > f;
}

语句中的"<",">"可以被解释成大于、小于。

针对这些有歧义的自定义template(boost::function实际可以正常被编译期识别,但用户自定义template不行),template的几种用法:

  • t::template f(); // call a function template
  • this->template f(); // call a function template
  • obj.template f(); // call a function template

引用

(C++) C++ template笔记 -- template关键字及typename关键字的更多相关文章

  1. typedef和typename关键字

    .类型说明typedef 类型说明的格式为: typedef 类型 定义名; 类型说明只定义了一个数据类型的新名字而不是定义一种新的数据类型.定义名表示这个类型的新名字. 例如: 用下面语句定义整型数 ...

  2. <转>详解C++的模板中typename关键字的用法

    用处1, 用在模板定义里, 标明其后的模板参数是类型参数. 例如: template<typename T, typename Y> T foo(const T& t, const ...

  3. C++中的关键字用法---typename

    1. typename 关键字 "typename"是一个C++程序设计语言中的关键字.当用于泛型编程时是另一术语"class"的同义词.这个关键字用于指出模板 ...

  4. C++ 模板template和template

    原文链接:https://blog.csdn.net/skyleung/article/details/42195509 template<class T>和template<typ ...

  5. typename 关键字

    1.class关键字的同义词 template <typename T> const T& max(const T& x, const T& y) { return ...

  6. C++中typename关键字的用法

    我在我的 薛途的博客 上发表了新的文章,欢迎各位批评指正. C++中typename关键字的用法

  7. Django.template框架 template context (非常详细)

    前面的章节我们看到如何在视图中返回HTML,但是HTML是硬编码在Python代码中的 这会导致几个问题: 1,显然,任何页面的改动会牵扯到Python代码的改动 网站的设计改动会比Python代码改 ...

  8. 《Java编程思想》读书笔记-基本规范、注释、static关键字、import关键字

    扫一扫加我的微信公众号,和我一起打好Java的基础 本文作为构建第一个Java程序的番外篇二,主要跟大家伙儿从浅层次的探讨下Java中的关键字import和static,此外为了让我们的代码可读性更强 ...

  9. Error resolving template [xxx], template might not exist or might not be exist

    Springboot+thymeleaf+mybatis 抛Error resolving template [xxx], template might not exist的异常 原因是我们在pom. ...

  10. Error resolving template: template might not exist or might not be accessible是一句缩水报错?

    一 thymeleaf在开发的时候本地调试正常,但是在测试环境打成jar包就报这个错误了. 二 template might not exist or might not be accessible ...

随机推荐

  1. 全志H616基于官方外设开发-蜂鸣器

    #include <stdio.h> #include <wiringPi.h> #include <unistd.h> #define BEEP 0 //设置针脚 ...

  2. 我的Vue之旅、04 CSS媒体查询完全指南(Media Quires)

    什么是SCSS Sass: Sass Basics (sass-lang.com) SCSS 是 CSS 的预处理器,它比常规 CSS 更强大. 可以嵌套选择器,更好维护.管理代码. 可以将各种值存储 ...

  3. Django 创建 APP和目录结构介绍

    一.通过pip安装Django 以windows 系统中使用pip命令安装为例 win+r,调出cmd,运行命令:pip install django自动安装PyPi 提供的最新版本.指定版本,可使用 ...

  4. js从数组中找到某个对象

    handel(ide) { let that = this; console.log(that.goodCaseList); let detailData = that.goodCaseList.fi ...

  5. Spring让人眼前一亮的11个小技巧

    前言 我们一说到spring,可能第一个想到的是 IOC(控制反转) 和 AOP(面向切面编程). 没错,它们是spring的基石,得益于它们的优秀设计,使得spring能够从众多优秀框架中脱颖而出. ...

  6. Linux命令全解

    strace 获取某个可执行文件执行过程中用到的所有系统调用 :strace -f g++ main.cpp &| vim 查看g++编译过程调用了哪些系统调用,通过管道符用vim接收 :%! ...

  7. Bert不完全手册9. 长文本建模 BigBird & Longformer & Reformer & Performer

    这一章我们来唠唠如何优化BERT对文本长度的限制.BERT使用的Transformer结构核心在于注意力机制强大的交互和记忆能力.不过Attention本身O(n^2)的计算和内存复杂度,也限制了Tr ...

  8. ubuntu20.04详细安装教程

    1.运行虚拟机,加载一段时间后弹出"安装"界面.语言栏倒数第三个是中文.点击[安装 Ubuntu]. 2.键盘布局,[继续] 3.更新和其他软件,取消[安装Ubuntu时下载更新] ...

  9. 使用idea操作git(ssh协议)

    问题 我们发现,使用IDEA上的git功能,当使用ssh协议出现了可以commit但无法push和pull的问题,经过测试发现原因是Could not read from remsitory.直接翻译 ...

  10. kali配置

    IP设置 配置文件:/etc/networking/interface 临时IP ifconfig eth0 192.168.31.111/24 永久IP auto eth0 #iface eth0 ...