转发声明constexpr变量模板 [英] Forward declare a constexpr variable template
问题描述
我试图像这样向前声明一个constexpr
变量模板:
I tried to forward-declare a constexpr
variable template like this:
template<typename>
constexpr std::size_t iterator_category_value;
目标是记录每个专业化名称应为constexpr
,但我不得不承认我从未检查过它是否合法,而g ++对此感到满意.但是,当我尝试使用clang ++编译此Spinnet时,出现以下错误:
The goal was to document that every specialization should be constexpr
but I have to admit that I never checked whether it was legal or not and g++ was happy with it. However, when I tried to compile this spinnet with clang++ instead, I got the following error:
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 ++似乎不同意,我想知道如果需要的话我应该在哪里提交错误报告.
The error makes sense, and removing constexpr
makes it disappear, so that's not a real problem. However, I am curious now: does the standard allow such a constexpr
forward declaration for a variable template or is it illegal? g++ and clang++ seem to disagree and I would like to know where I should submit a bug report if needed.
他们两个都抱怨一个前向声明的constepxr
变量,它不是变量模板,因此变量模板上下文似乎是导致编译器不同意的原因.
Both of them complain for a forward-declared constepxr
variable which is not a variable template, so the variable template context seems to be what makes the compilers disagree.
推荐答案
在C ++ 14标准中,很明显需要初始化.根据第7.5.1节第9段,
In the C++14 standard, it seems pretty clear that initialization is required. From section 7.5.1 paragraph 9,
对象声明中使用的
constexpr
说明符 将该对象声明为const.此类物体应具有 文字类型,应进行初始化.
A
constexpr
specifier used in an object declaration declares the object as const. Such an object shall have literal type and shall be initialized.
关于对象声明"的确切含义,第7条第7款规定:
As for the exact meaning of "object declaration", Section 7 paragraph 7 states:
如果decl-specifier-seq不包含typedef说明符, 该声明称为函数声明,如果 与名称关联的类型是函数类型,并且 否则是对象声明.
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.
这篇关于转发声明constexpr变量模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!