c++11 : Local and Unnamed Types as Template Arguments
In N2402, Anthony Williams proposes that local types, and unnamed types be usable as template arguments. At the February 2008 (Bellevue) meeting, the Core working group supported the use of local types but was concerned about unnamed types. In addition, the WG did not support the mechanism used to accomplish this change in N2402.
I discussed the unnamed type issue with Anthony Williams and others. Use of unnamed types as template arguments was considered a useful facility and does not seem to present any particular implementation issues. In particular, it is fairly common practice to use unnamed enumerations in header files. Consequently, unnamed types have been retained in this proposal. Note that a namespace scope unnamed enumeration in one translation unit is distinct from one in another translation unit. So in the example below, x and y are initalized by two different instances of f
file1.c: template <class T> int f(T t); enum {e1}; int x = f(e1); file2.c: template <class T> int f(T t); enum {e1}; int y = f(e1);
N2402 makes local types and unnamed types usable as template arguments by giving them linkage. As mentioned above, the Core Working Group did not favor this solution. This document provides drafting to allow local and unnamed types to be used as template arguments by revising the rules for template arguments to permit such types.
Working Paper Changes
Change 3.5 [basic.link] paragraph 8 as follows:
A type without linkage shall not be used as the type of a variable or function with linkage, unless
- the variable or function has extern "C" linkage
., or- the type without linkage was named using a dependent type (14.6.2.1).
Delete the following sentence from 3.5 [basic.link] paragraph 8:
This implies that names with no linkage cannot be used as template arguments (14.3).
Add the following example after paragraph 8:
[ Example: template <class T> struct A { // in A<X>, the following is allowed because the type with no linkage // X is named using template parameter T. friend void f(A, T){} }; template <class T> void g(T t) { A<T> at; f(at, t); } int main() { class X {} x; g(x); } -- end example]
In 14.3.1 [temp.arg.type] paragraph 2 remove the following text:
A type without linkage (3.5) shall not be used as a template argument for a template type parameter.
Replace the example in paragraph 2 with the following:
[ Example: template <class T> class X { }; template <class T> void f(T t) { } struct {} unnamed_obj; void f() { struct A { }; enum { e1 }; typedef struct {} B; B b; X<A> x1; // OK X<A*> x2; // OK X<B> x3; // OK f(e1); // OK f(unnamed_obj); // OK f(b); // OK } -- end example]
End of document.
c++11 : Local and Unnamed Types as Template Arguments的更多相关文章
- C++ Templates (1.4 默认模板实参 Default Template Arguments)
返回完整目录 目录 1.4 默认模板实参 Default Template Arguments 1.4 默认模板实参 Default Template Arguments 可以为模板参数定义默认值,这 ...
- Std::map too few template arguments
在上述的代码中,红色波浪线的部分编译的时候报错: error C2976: 'std::map' : too few template arguments 换成std::map<std::str ...
- tornado学习笔记11 Web应用中模板(Template)使用应用实践
上一篇中(Web应用中模板的工作流程分析),已经分析了模板的渲染流程,以及相关参数获取及设置原理.这篇主要讲述模板在实际应用案例. 11.1 需求 根据用户输入的两次密码,判断两次密码是否一致,并将判 ...
- (11)zabbix item types监控类型
1. 什么是item types item types是由zabbix提供的各种类型的检查器(这样翻译很奇怪),大致就是Zabbix agent, Simple checks, SNMP, Zabbi ...
- C++学习三 模板类出错总结(Missing template arguments before 'L')
一.模板类的说明 模板类有一个好处是可以放宽你输入的数据类型. 比如有这样的一个函数: int add(int x, int y) { return x+y; } 这个函数对于int类型的x,y才适合 ...
- C++11 能好怎?
0. 摘要 近期读了一些关于C++11标准的材料. 本篇博客将从新标准的优点.与旧版本的区别和使用方法三个角度,大致介绍我对C++11的认识. C++11标准,原名C++0x, 是03版旧标准的更新. ...
- c++11介绍
C++11标准是 ISO/IEC 14882:2011 - Information technology -- Programming languages -- C++ 的简称[1] . C++11 ...
- VS2010对C++11的支持列表(感觉大部分都不支持)
c++11,就是之前的c++0x,已经成为了最新的c++标准.像咱这样天天用c++的,就赶紧follow一下.学习成果,放在这里,不说分享,至少自己增强下记忆. 首先,给出一些有用的链接. http: ...
- C++ Standards Support in GCC - GCC 对 C++ 标准的支持
C++ Standards Support in GCC - 2019-2-20 GCC supports different dialects of C++, corresponding to th ...
随机推荐
- C#获取显示器宽度高度,桌面宽度高度等
1.C#获取显示器宽度高度,桌面宽度高度等 //获取当前显示器的宽度和高度 int width = Screen.PrimaryScreen.Bounds.Width; int height = Sc ...
- (转)jQuery Validate 表单验证
在做网页表单时时常需要在客户端对表单填写的数据进行验证一番才能提交,我们可以通过自己编写JavasScript代码来验证,但是有时数据量过多时就会有些难度了.基于jQuery的jquery.valid ...
- MySQL数据库my.cnf配置文件注释详解
我们知道,在MySQL数据库安装完成后,要对my.cnf配置文件进行适当的修改才能充分利用MySQL数据库的功能.但是对于初学者来说,修改my.cnf配置文件似乎是一个比较难的过程.为了解决这个问题, ...
- Cer Crt Pem Pfx 证书格式转换
1.从pfx格式的证书提取出密钥和证书set OPENSSL_CONF=openssl.cnfopenssl pkcs12 -in my.pfx -nodes -out server.pemopens ...
- MySQL 行子查询
MySQL 行子查询 行子查询是指子查询返回的结果集是一行 N 列,该子查询的结果通常是对表的某行数据进行查询而返回的结果集. 一个行子查询的例子如下: SELECT * FROM table1 WH ...
- C++拾遗(八)类——概念、定义与实现
Class与Struct 区别在于class默认访问类型是private,struct默认访问类型是public. 另外在使用习惯上,struct只用来表示纯粹的数据对象或没有私有部分的类. 类中的内 ...
- C/C++安全编码-字符串
1 字符串 1.1 字符串基础 字符串提供命令行参数.环境变量.控制台输入.文本文件及网络连 接,提供外部输入方法来影响程序的行为和输出,这也是程序容易出错的地方.字符串是一个概念,并不是C/ ...
- Linux程序设计笔记
使用size命令查看二进制文件时,结果并不一定和预测占用内存大小一致,因为可能存在内存对齐,导致内存字节数比实际的更多 在C语言中,字符串常量存放在text segment中,在C++中却是存储在da ...
- jQuery自学笔记(一):初识jQuery
jQuery 是一个 JavaScript 函数库, jQuery 库位于一个 JavaScript 文件中,其中包含了所有的 jQuery 函数,引用jQuery应该注意: <script&g ...
- python ciscolib模块
python的第三方模块ciscolib可以登录交换机执行相关命令 ciscolib下载地址: https://github.com/nickpegg/ciscolib 常见用法如下: switch ...