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的更多相关文章

  1. C++ Templates (1.4 默认模板实参 Default Template Arguments)

    返回完整目录 目录 1.4 默认模板实参 Default Template Arguments 1.4 默认模板实参 Default Template Arguments 可以为模板参数定义默认值,这 ...

  2. Std::map too few template arguments

    在上述的代码中,红色波浪线的部分编译的时候报错: error C2976: 'std::map' : too few template arguments 换成std::map<std::str ...

  3. tornado学习笔记11 Web应用中模板(Template)使用应用实践

    上一篇中(Web应用中模板的工作流程分析),已经分析了模板的渲染流程,以及相关参数获取及设置原理.这篇主要讲述模板在实际应用案例. 11.1 需求 根据用户输入的两次密码,判断两次密码是否一致,并将判 ...

  4. (11)zabbix item types监控类型

    1. 什么是item types item types是由zabbix提供的各种类型的检查器(这样翻译很奇怪),大致就是Zabbix agent, Simple checks, SNMP, Zabbi ...

  5. C++学习三 模板类出错总结(Missing template arguments before 'L')

    一.模板类的说明 模板类有一个好处是可以放宽你输入的数据类型. 比如有这样的一个函数: int add(int x, int y) { return x+y; } 这个函数对于int类型的x,y才适合 ...

  6. C++11 能好怎?

    0. 摘要 近期读了一些关于C++11标准的材料. 本篇博客将从新标准的优点.与旧版本的区别和使用方法三个角度,大致介绍我对C++11的认识. C++11标准,原名C++0x, 是03版旧标准的更新. ...

  7. c++11介绍

    C++11标准是 ISO/IEC 14882:2011 - Information technology -- Programming languages -- C++ 的简称[1]  . C++11 ...

  8. VS2010对C++11的支持列表(感觉大部分都不支持)

    c++11,就是之前的c++0x,已经成为了最新的c++标准.像咱这样天天用c++的,就赶紧follow一下.学习成果,放在这里,不说分享,至少自己增强下记忆. 首先,给出一些有用的链接. http: ...

  9. C++ Standards Support in GCC - GCC 对 C++ 标准的支持

    C++ Standards Support in GCC - 2019-2-20 GCC supports different dialects of C++, corresponding to th ...

随机推荐

  1. [linux常用命令]查看当前文件夹或该文件夹下文件(夹)的大小

    du -sh  *(星号表示当前所有文件夹)可以查看当前目录下各个文件夹的大小,-s表示只显示当前文件夹(不加-s你可以看到所有文件夹下的子文件夹的大小,太多了),-h表示以合适的大小查看.(可以用- ...

  2. 产品经理(PM)常用原型图设计工具

    本文转贴自:http://www.zhangping.name/2010/08/28/pm-wireframes-design-tools/ ,尽管都是一些商业软件,但对设计的确非常有帮助. 天天和产 ...

  3. Mac系统配置多个git账号

    1.进入ssh目录 #cd ~/.ssh/ 2.用ssh-keygen命令生成一组新的id_rsa_new和id_rsa_new.pub #ssh-keygen -t rsa -C"new ...

  4. Swift - 35 - 使用闭包简化语法

    //: Playground - noun: a place where people can play import UIKit // 初始化一个整数数组 var arr = [1, 3, 5, 7 ...

  5. Mysql 视图笔记2

    这学期开了数据库的课,对sql注入颇感兴趣.因此,对数据库语句也颇为喜爱.遇到了with check option 问题.这属于sql view里面的一个问题.在此略做小结.大牛勿喷! 先自定义一个t ...

  6. ActiveRecord 模式杂谈

    ActiveRecord也属于ORM(对象关系映射)层,由Rails最早提出,遵循标准的ORM模型:表映射到记录,记录映射到对象,字段映射到对象属性.配合遵循的命名和配置惯例,能够很大程度的快速实现模 ...

  7. d029: 求出2-100之间的所有质数(素数)

    内容: 求出2-100之间的所有质数(素数) 输入说明: 无 输出说明: 一行一个素数 /* 质数又称素数.指在一个大于1的自然数中,除了1和此整数自身外,不能被其他自然数(不包括0)整除的数. */ ...

  8. codevs 1068 乌龟棋

    题目描述 Description 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一 的起点,第N格是终点,游戏要求玩家控制 ...

  9. IOS APP IDs

    感谢分享 说下 APP ID 的组成为  APP ID Prefix + APP ID suffix APP ID Prefix 为10个字符  你不用管它 APP ID Suffix  这个有点讲究 ...

  10. Cracking the coding interview--Q1.6

    原文: Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a m ...