模板函数中的静态对象是否具有链接? [英] Does static object in a template function have linkage?

查看:141
本文介绍了模板函数中的静态对象是否具有链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是指我上一个问题: clang不编译,但g ++没有。从我的研究,关键的问题归结为链接,静态变量数据在下面的示例中有链接(它编译与 g ++ - 4.8 .1 )?如何具有链接(我将无法用非类型模板参数实例化)?

This question refers to my previous question: clang does not compile but g++ does. From my research, the issue at stake boils down to linkage, does the static variable data have linkage in the sample below (it compiles with g++-4.8.1)? How come it has linkage (I would not otherwise be able to instantiate with a non-type template parameter)?

template <int const* ptr>
void foo()
{
}

typedef void (*func_type)();

template <int = 0>
void run_me()
{
  static int data;

  func_type const f1 = foo<&data>;
  // auto f2 = foo<&data>; // doesn't work with gcc
  // foo<&data>();         // doesn't work with gcc
}

int main(int, char*[])
{
  run_me();

  return 0;
}

标准的强制报价:

非类型非模板模板参数的模板参数应为以下之一:
...

A template-argument for a non-type, non-template template-parameter shall be one of: ...

- a指定具有静态存储持续时间的对象的地址的常量表达式(5.19)以及
外部或内部链接或具有外部或内部链接的函数,包括函数模板
和函数模板id,静态类成员,表达(忽略括号)为
& id表达式,除了&如果名称引用函数或数组,则可以省略,并且如果相应的模板参数是引用,则
被省略;或
...

— a constant expression (5.19) that designates the address of an object with static storage duration and external or internal linkage or a function with external or internal linkage, including function templates and function template-ids but excluding non-static class members, expressed (ignoring parentheses) as & id-expression, except that the & may be omitted if the name refers to a function or array and shall be omitted if the corresponding template-parameter is a reference; or ...

推荐答案

当然在函数中声明了一个静态变量(无论它是一个模板函数)没有联系。

Surely a static variable declared in a function (whether or not it is a template function) has no linkage.

§ 3.5 para。 8:除非注明,在块范围(3.3.3)声明的名称没有连接

§3.5 para. 8: "except as noted, a name declared at block scope (3.3.3) has no linkage"

在该子句中列出的唯一例外,见第19段。 6:块范围中声明的函数的名称和块范围extern声明声明的变量的名称具有链接。

The only exceptions listed in that clause, as far as I can see, are provided in para. 6: "The name of a function declared in block scope and the name of a variable declared by a block scope extern declaration have linkage."

但是,可能是14.3 .2在某些时候可能会放松。

However, it's possible that 14.3.2 might be loosened at some point.

DanielKrügler提交了 DR 1451 on 2012-02-01:

Daniel Krügler submitted DR 1451 on 2012-02-01:


到14.3.2 [temp.arg.nontype]段落1项目符号3,只有具有链接的对象可以用于形成非类型模板参数。这个限制是否仍然需要?使用块范围对象作为模板参数很方便。

According to 14.3.2 [temp.arg.nontype] paragraph 1 bullet 3, only objects with linkage can be used to form non-type template arguments. Is this restriction still needed? It would be convenient to use block-scope objects as template arguments.

DR是关闭的,因为它是一个扩展请求并应由进化工作组处理。它似乎已包含在 n3413 ,允许非类型模板参数的任意文字类型。

The DR was closed on the basis that it is an extension request and should be dealt with by the Evolution Working Group. It seems to have been included in n3413, "Allowing arbitrary literal types for non-type template parameters".

因此,一个或多个C ++编译器可能会选择对非类型模板参数实施更宽松的限制,类型模板参数。

So it's certainly conceivable that one or more C++ compilers might choose to implement a looser restriction on non-type template parameters.

这篇关于模板函数中的静态对象是否具有链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆