Movatterモバイル変換


[0]ホーム

URL:


Document number: 08-0167/N2657
Date: 2008-06-10
Author: John Spicer, Edison Design Group
 jhs@edg.com

Local and Unnamed Types as Template Arguments

Introduction

In N2402, Anthony Williams proposes that local types, and unnamed types beusable as template arguments. At the February 2008 (Bellevue) meeting, theCore working group supported the use of local types but was concernedabout unnamed types. In addition, the WG did not support the mechanism usedto 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 facilityand does not seem to present any particular implementation issues. Inparticular, it is fairly common practice to use unnamed enumerations inheader files. Consequently, unnamed types have been retained in thisproposal.Note that a namespace scope unnamedenumeration in one translation unit is distinct from one in anothertranslation unit. So in the example below,x andyare initalized by two different instances off

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 bygiving them linkage. As mentioned above, the Core Working Group did notfavor this solution.This document provides drafting to allow local and unnamed types to be usedas template arguments by revising the rules for template arguments to permitsuch 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 orfunction with linkage, unless
  • the variable or function has extern "C" linkage., or
  • the type without linkagewas 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 templatearguments (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 beused 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.


    [8]ページ先頭

    ©2009-2025 Movatter.jp