c++ - 作为非类型模板参数的引用

标签 c++ c++11 language-lawyer

下面的示例尝试使用引用类型的变量作为非类型模板形参(其本身为引用类型)的实参。 Clang、GCC 和 VC++ 都拒绝它。但为什么?我似乎在标准中找不到任何使其非法的内容。

int obj = 42;
int& ref = obj;

template <int& param> class X {};

int main()
{
    X<obj> x1;  // OK
    X<ref> x2;  // error
}

Live example


CLang 说:

source_file.cpp:9:7: error: non-type template argument of reference type 'int &' is not an object

其他人也以类似的方式提示。


来自标准(所有引用来自 C++11;C++14 的相关部分似乎没有重大变化):

14.3.2/1 A template-argument for a non-type, non-template template-parameter shall be one of:

...

  • a constant expression (5.19) that designates the address of an object with static storage duration and external or internal linkage ... expressed (ignoring parentheses) as & id-expression, except that the & ... shall be omitted if the corresponding template-parameter is a reference

...

现在什么是常量表达式:

5.19/2 A conditional-expression is a core constant expression unless it involves one of the following as a potentially evaluated subexpression (3.2)...

...

  • an id-expression that refers to a variable or data member of reference type unless the reference has a preceding initialization, initialized with a constant expression

...


据我所知,refX<ref>是一个id-expression,它引用一个引用类型的变量。这个变量有一个前面的初始化,用表达式 obj 初始化。 .我相信obj是一个常量表达式,如果不是,那么 X<obj>也不应该编译。

  • 那我错过了什么?
  • 标准中的哪个条款呈现 X<ref>无效,而 X<obj>有效吗?

最佳答案

简介

虽然reference 的名称是一个id-expression 是正确的; id-expression 不引用引用所引用的任何内容,而是引用本身。

 int    a = 0;
 int& ref = a; // "ref" is an id-expression, referring to `ref` - not `a`

标准(N4140)

您在帖子中引用了标准的相关部分,但您遗漏了最重要的部分(强调我的部分):

14.3.2p1 Template non-type arguments [temp.arg.nontype]

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

  • ...

  • a constant expression (5.19) that designates the address of a complete object with static sturage 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, where id-expression is the name of an object or function, 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; ...


注意:在较早的草稿中“其中 id-expression 是对象或函数的名称” 不存在;由 DR 1570 解决- 这无疑使意图更加清晰。


引用类型的变量不是对象

你完全正确;引用本身具有引用类型,并且作为表达式的一部分只能充当对象。

5p5 Expressions [expr]

If an expression initially has the type "reference to T" (8.3.2, 8.5.3), the type is adjusted to T prior to any further analysis. The expression designates the object or function denoted by the reference, and the expression is an lvalue or an xvalue, depending on the expression.


细化

非常重要的是要注意常量表达式(“表示一个完整对象的地址...”)必须是&id之一-表达式,或id-expression

即使一个constant-expression,不仅仅是一个id-expression,可能引用一个具有静态存储持续时间的对象,我们不能用它来< em>“初始化”一个模板参数reference-指针类型

示例片段

template<int&>
struct A { };

int            a = 0;
constexpr int& b = (0, a); // ok, constant-expression
A<(0, a)>      c = {};     // ill-formed, `(0, a)` is not an id-expression

注意:这也是我们不能使用string-literals作为template-arguments的原因;它们不是 id 表达式

关于c++ - 作为非类型模板参数的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28662784/

相关文章:

c++ - 获取错误 : 'mutex' in namespace 'std' does not name a type in MinGW mysys prompt

c - "thread storage duration"和 "thread local storage duration"是同义词吗?

c++ - 如何为文本模板创建 emacs 宏

c++ - 在 C++ 中将一 block 内存分配给多维数组

c++ - 在它自己的析构函数中移动 unique_ptr 是否可以?

c++ - `*--p` 在 C++03 中实际上是否合法(格式正确)

空指针常量可以是任何取值为0的整数常量表达式吗?

c++ - 如何使用 boost::function_types::parameter_types 与 ClassTypeTransform

android - 启用异常 C++

c++ - 如何初始化 std::array<std::array<T, 2>, 2> 的对象?