- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
注意:原题大量使用宏,但本题已简化。
// header.hpp
template <typename T>
void foo()
{
someBoolean = true ; // at this point "someBoolean" wasn't
} // declared
然后,它在以下来源中使用:
// source.cpp
#include "header.hpp"
static bool someBoolean = false ;
void bar()
{
foo<char>() ; // here, we call/instantiate the function
}
在某些编译器(Windows、以前的 Solaris)中它可以工作。而在当前启用 C++11 的 Solaris 编译器中,它会失败,说 someBoolean 未定义。
根据标准,模板化代码可以使用(我们希望!)稍后在源代码中声明的变量吗?
模板在一个头文件中定义,该头文件预计将包含在多个源中,每个源都有自己的 bool 变量,并实例化模板。
预计,在每个翻译单元中,模板都会影响该翻译单元的静态 bool 变量。
因此,预计在一种类型(例如“char”)上的模板的每个实例化都会影响不同的变量。
我们不是依赖未定义的行为吗?
最佳答案
这与两阶段查找有关。简短的版本是,任何 not 依赖于模板参数的名称(如 someBoolean
此处)将在模板定义时查找。这意味着 Solaris 编译器拒绝代码是正确的。 someBoolean
在定义模板之前没有定义。
确实依赖于模板参数的名称(例如,如果您编写了类似 T::someBoolean = true
之类的东西)将推迟到模板实例化时间 - 非常合理,因为它们的有效性不能在编译器知道 T
是什么之前确定。 MSVC 以没有正确实现这些两阶段语义而闻名(至少从历史上看),这就是您的代码在那里工作的原因。不过,这不是正确的 C++,也不是可移植的行为。
C++ 标准(可能是草案)(未知版本)的第 14.6 节:
If a name does not depend on a template-parameter (as defined in 14.6.2), a declaration (or set of declarations) for that name shall be in scope at the point where the name appears in the template definition; the name is bound to the declaration (or declarations) found at that point and this binding is not affected by declarations that are visible at the point of instantiation.
(通过下面的@BenVoigt 评论)
关于c++ - 在模板函数中使用未声明的变量是否合法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29541875/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!