- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
这是一个非常基本的问题,但我不明白为什么下面的代码不能在 GCC 4.6.1 上编译。它确实在带有 SP1 的 VS 2008 上编译:
#include <iostream>
class MyClass
{
public:
const static int MinValue = -1000;
const static int MaxValue = 1000;
};
void printValue(int i)
{
std::cout << i << std::endl;
}
int main(int argc, char** argv)
{
printValue(MyClass::MinValue);
printValue(MyClass::MaxValue);
printValue(argc < 42 ? MyClass::MinValue : MyClass::MaxValue); //This line gives the error
}
GCC 说:
david@David-Laptop:~/temp$ g++ test.cpp
/tmp/ccN2b95G.o: In function `main':
test.cpp:(.text+0x54): undefined reference to `MyClass::MinValue'
test.cpp:(.text+0x5c): undefined reference to `MyClass::MaxValue'
collect2: ld returned 1 exit status
但是,如果我对“printValue”进行第三次调用,那么它会正确构建并运行。所以这与“?”有关。运算符(operator)……这样用是不是无效?此外,如果我将 'argc < 42' 替换为 'true' 或 'false' 它也可以正常构建。
有什么想法吗?!
最佳答案
根据“一个定义规则”,如果变量是odr-used,则变量必须只有一个定义。这是由 C++11 标准定义的:
3.2/2 A variable or non-overloaded function whose name appears as a potentially-evaluated expression is odr-used unless it is an object that satisfies the requirements for appearing in a constant expression and the lvalue-to-rvalue conversion is immediately applied.
以及 ODR 本身:
3.2/3 Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required.
作为函数调用参数,它们不是odr-used:它们是在声明中指定值的常量,因此可以出现在常量表达式中;它们是按值传递的,因此会立即转换为右值。
在条件表达式中使用它们时不是这种情况。由于两者都是指向同一类型的左值,因此根据定义条件运算符的相当复杂的规则之一,条件表达式的结果将是一个左值:
5.16/4 If the second and third operands are glvalues of the same value category and have the same type, the result is of that type and value category.
(此规则允许像 (a?b:c)=d
这样的表达式。)
所以常量本身并不会立即转换为右值,并且由于运行时条件,条件表达式不能出现在常量表达式中;因此,它们是 odr-used,因此需要一个定义。
正如您所注意到的,将条件更改为常量表达式可以修复链接错误;改变一个常数的类型也是如此。但目前的表达方式要求它们在一个(并且只有一个)翻译单元中有一个定义:
const int MyClass::MinValue;
const int MyClass::MaxValue;
关于c++ - 奇怪的 GCC 编译错误(包括简单示例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8879944/
我正在努力实现以下目标, 假设我有字符串: ( z ) ( A ( z ) ( A ( z ) ( A ( z ) ( A ( z ) ( A ) ) ) ) ) 我想编写一个正则
给定: 1 2 3 4 5 6
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
大家好,我卡颂。 Svelte问世很久了,一直想写一篇好懂的原理分析文章,拖了这么久终于写了。 本文会围绕一张流程图和两个Demo讲解,正确的食用方式是用电脑打开本文,跟着流程图、Demo一
身份证为15位或者18位,15位的全为数字,18位的前17位为数字,最后一位为数字或者大写字母”X“。 与之匹配的正则表达式: ?
我们先来最简单的,网页的登录窗口; 不过开始之前,大家先下载jquery的插件 本人习惯用了vs2008来做网页了,先添加一个空白页 这是最简单的的做法。。。先在body里面插入 <
1、MySQL自带的压力测试工具 Mysqlslap mysqlslap是mysql自带的基准测试工具,该工具查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出
前言 今天大姚给大家分享一款.NET开源(MIT License)、免费、简单、实用的数据库文档(字典)生成工具,该工具支持CHM、Word、Excel、PDF、Html、XML、Markdown等
Go语言语法类似于C语言,因此熟悉C语言及其派生语言( C++、 C#、Objective-C 等)的人都会迅速熟悉这门语言。 C语言的有些语法会让代码可读性降低甚至发生歧义。Go语言在C语言的
我正在使用快速将 mkv 转换为 mp4 ffmpeg 命令 ffmpeg -i test.mkv -vcodec copy -acodec copy new.mp4 但不适用于任何 mkv 文件,当
我想计算我的工作簿中的工作表数量,然后从总数中减去特定的工作表。我错过了什么?这给了我一个对象错误: wsCount = ThisWorkbook.Sheets.Count - ThisWorkboo
我有一个 perl 文件,用于查看文件夹中是否存在 ini。如果是,它会从中读取,如果不是,它会根据我为它制作的模板创建一个。 我在 ini 部分使用 Config::Simple。 我的问题是,如果
尝试让一个 ViewController 通过标准 Cocoa 通知与另一个 ViewController 进行通信。 编写了一个简单的测试用例。在我最初的 VC 中,我将以下内容添加到 viewDi
我正在绘制高程剖面图,显示沿路径的高程增益/损失,类似于下面的: Sample Elevation Profile with hand-placed labels http://img38.image
嗨,所以我需要做的是最终让 regStart 和 regPage 根据点击事件交替可见性,我不太担心编写 JavaScript 函数,但我根本无法让我的 regPage 首先隐藏。这是我的代码。请简单
我有一个非常简单的程序来测量一个函数花费了多少时间。 #include #include #include struct Foo { void addSample(uint64_t s)
我需要为 JavaScript 制作简单的 C# BitConverter。我做了一个简单的BitConverter class BitConverter{ constructor(){} GetBy
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是 Simple.Data 的新手。但我很难找到如何进行“分组依据”。 我想要的是非常基本的。 表格看起来像: +________+ | cards | +________+ | id |
我现在正在开发一个 JS UDF,它看起来遵循编码。 通常情况下,由于循环计数为 2,Alert Msg 会出现两次。我想要的是即使循环计数为 3,Alert Msg 也只会出现一次。任何想法都
我是一名优秀的程序员,十分优秀!