转发声明一个 constexpr 变量模板

Forward declare a constexpr variable template

我尝试像这样转发声明一个 constexpr 变量模板:

template<typename>
constexpr std::size_t iterator_category_value;

目标是记录每个专业化都应该是 constexpr 但我不得不承认我从未检查过它是否合法并且 g++ 对此很满意。然而,当我试图用 clang++ 编译这个 spinnet 时,我得到了以下错误:

error: default initialization of an object of const type 'const std::size_t' (aka 'const unsigned long')
    constexpr std::size_t iterator_category_value;
                          ^
                                                  = 0

错误是有道理的,删除 constexpr 会使错误消失,所以这不是真正的问题。但是,我现在很好奇:标准是否允许对变量模板进行这样的 constexpr 前向声明还是非法的? g++ 和 clang++ 似乎不同意,我想知道如果需要我应该在哪里提交错误报告。

他们都抱怨前向声明的 constepxr 变量不是变量模板,因此变量模板上下文似乎是编译器不同意的原因。

在C++14标准中,似乎很清楚需要初始化。来自第 7.5.1 节第 9 段,

A constexpr specifier used in an object declaration declares the object as const. Such an object shall have literal type and shall be initialized.

至于"object declaration"的确切含义,Section 7 paragraph 7 states:

If the decl-specifier-seq contains no typedef specifier, the declaration is called a function declaration if the type associated with the name is a function type and an object declaration otherwise.

叮当是正确的。变量模板的 声明 是对象声明 ([dcl.dcl]/9),因此它必须根据 [dcl.constexpr]/9 提供初始化程序:

A constexpr specifier used in an object declaration declares the object as const. Such an object […] shall be initialized.

不过,实际上没有办法 "forward" 首先将对象声明为 constexpr;如果constexpr应用于变量的声明,它应该是一个定义([dcl.constexpr]/1).