- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我今天遇到了一个有趣的问题。考虑这个简单的例子:
template <typename T>
void foo(const T & a) { /* code */ }
// This would also fail
// void foo(const int & a) { /* code */ }
class Bar
{
public:
static const int kConst = 1;
void func()
{
foo(kConst); // This is the important line
}
};
int main()
{
Bar b;
b.func();
}
编译时出现错误:
Undefined reference to 'Bar::kConst'
现在,我很确定这是因为 static const int
没有在任何地方定义,这是故意的,因为根据我的理解,编译器应该能够在编译时进行替换-时间,不需要定义。但是,由于该函数采用 const int &
参数,它似乎没有进行替换,而是更喜欢引用。我可以通过进行以下更改来解决此问题:
foo(static_cast<int>(kConst));
我相信这现在迫使编译器生成一个临时 int,然后传递一个对它的引用,它可以在编译时成功地做到这一点。
我想知道这是否是故意的,还是我对 gcc 的期望太高而无法处理这种情况?或者这是出于某种原因我不应该做的事情?
最佳答案
这是故意的,9.4.2/4 说:
If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression (5.19) In that case, the member can appear in integral constant expressions. The member shall still be defined in a namespace scope if it is used in the program
当您通过 const 引用传递静态数据成员时,您“使用”它,3.2/2:
An expression is potentially evaluated unless it appears where an integral constant expression is required (see 5.19), is the operand of the sizeof operator (5.3.3), or is the operand of the typeid operator and the expression does not designate an lvalue of polymorphic class type (5.2.8). An object or non-overloaded function is used if its name appears in a potentially-evaluated expression.
所以事实上,当你通过值或在 static_cast
中传递它时,你也“使用”了它。只是 GCC 在一种情况下让你摆脱了困境,但在另一种情况下却没有。
[编辑:gcc 正在应用 C++0x 草案中的规则:“名称显示为潜在评估表达式的变量或非重载函数是 odr-used,除非它是满足出现要求的对象在常量表达式 (5.19) 中,立即应用左值到右值的转换 (4.1)。”。静态转换立即执行左值-右值转换,因此在 C++0x 中它没有被“使用”。]
const 引用的实际问题是 foo
有权获取其参数的地址,并将其与另一个调用的参数地址进行比较,例如存储在全局的。由于静态数据成员是唯一的对象,这意味着如果您从两个不同的 TU 调用 foo(kConst)
,那么在每种情况下传递的对象的地址必须相同。除非对象在一个(并且只有一个)TU 中定义,否则 AFAIK GCC 无法安排。
好的,所以在这种情况下,foo
是一个模板,因此定义在所有 TU 中都是可见的,所以编译器理论上可以排除它对地址做任何事情的风险。但总的来说,您当然不应该获取不存在对象的地址或引用;-)
关于c++ - 对静态 const int 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5391973/
标记为家庭作业,因为这是我写的期中问题,但我不明白答案。我被要求在以下语句中解释每个 const 的用途: const char const * const GetName() const { ret
const int* const fun(const int* const& p) const; 我试图弄清楚这个给定函数原型(prototype)的输入参数。我在这两个之间争论,不确定哪个是正确的。
下面的代码用于在同时存在 const 和非 const getter 时减少代码重复。它从非 const 创建 const 版本。我搜索了一下,很多人说我应该从 const 创建非 const 版本。
据我所知,TypeScript 查看了 const string变量作为一个不可变的类型变量,只有那个值,没有其他可能的值。一直以为加as const那是多余的。 为什么我在示例的第二部分得到以下内容
我有一个具有以下签名的方法: size_t advanceToNextRuleEntryRelatedIndex( size_t index, size_t nStrings, char const
首先,有什么区别: (1) const char* (2) char const* (3) const char const* 我相当确定我完全理解这一点,但我希望有人能具体地给我一个句子,这样它就会
这里是新手! 我正在阅读一段代码,我看到作者经常写一个成员函数作为 const int func (const scalar& a) const // etc 你看这里有三个const,现在我明白了中
我总是搞乱如何正确使用 const int*、const int * const 和 int const *。是否有一套规则来定义你可以做什么和不能做什么? 我想知道在赋值、传递给函数等方面所有该做和
我见过人们将 const 用作函数参数的代码。使用 const* 与 const * const 有什么好处?这可能是一个非常基本的问题,但如果有人能解释一下,我将不胜感激。 Bool IsThisN
我总是搞乱如何正确使用 const int*、const int * const 和 int const *。是否有一套规则来定义你可以做什么和不能做什么? 我想知道在赋值、传递给函数等方面所有该做和
这个问题在这里已经有了答案: What is the difference between const int*, const int * const, and int const *? (23 个
如果引用的对象不是 const 对象,那么引用“const”关键字的目的是什么? r1 和 r2 的作用(如下)有什么不同吗? int i = 42; // non const object cons
friend 让我解释原因 const const const const const int const i = 0; 是有效的语法。我拒绝对这个话题有任何想法。虽然我很好奇它是否只是语法问题? 编
我总是搞砸如何正确使用 const int*、const int * const 和 int const *。是否有一套规则来定义你能做什么和不能做什么? 我想知道在分配、传递给函数等方面的所有注意事
常量在 const char* push(const char * const &&_data); 表示无法更改引用的内容。为什么我不能将 const char* 传递给 push? 最佳答案 您的代
我有一个关于在函数参数中涉及指针的最佳实践以及它们是否应该指定为 *const 的问题或 const *const .我知道对于 const 的使用或过度使用存在不同的意见。 ,但至少有一些用途是捕捉
我目前正在为我的类(class)写一个作业,它应该充当一个非常基本的外壳。我快完成了,但是我遇到了 execvp 和我的参数字符数组的问题。这是我的代码的一小段。 //Split the left c
所以,我知道了char const *、char * const 和char const * const 之间的区别。那些是: char* the_string : I can change the
我正在运行一些示例程序以重新熟悉 C++,我遇到了以下问题。首先,这里是示例代码: void print_string(const char * the_string) { cout << t
我正在为系统中的编译错误而苦苦挣扎,这是代码 struct Strless : public binary_function { public : bool operator()(cons
我是一名优秀的程序员,十分优秀!