C++标准是否隐式或明确地允许这样的语言扩展(或使用您喜欢的任何其他术语)作为MOC?
也就是说,我们可以在技术上将Qt(包括MOC)称为符合C++的实现吗?
可能重复:
未定义的行为和序列点
变量i
被更改两次,但下一个示例是否会导致未定义的行为?
#include <iostream>
int main()
{
int i = 5;
std::cout << "before i=" << i << std::endl;
++ i %= 4;
std::cout << "after i=" << i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
before i=5
after i=2
Run Code Online (Sandbox Code Playgroud) 我正在为我正在上课的代码编写代码.我不能发布我的所有代码而不归零我的项目得分,但这里是我的驱动程序的缩写代码:
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include "Stack.h"
using namespace std;
namespace jack
{
int high(char a)
{
// My Code
};
bool isSameOrHigher(char top, char cur)
{
// My Code
};
int main()
{
// My Code
};
};
Run Code Online (Sandbox Code Playgroud)
由于某种原因,我无法弄清楚何时编译此代码,我收到以下错误:
链接:致命错误LNK1561:必须定义入口点
现在,据我所知,只有当我没有主要功能时才会出现这个错误,你可以看到我确实有这个功能.我已经尝试将此文件中的代码复制到另一个项目中,我已经尝试将我的main函数单独分离到另一个cpp文件中(导致更多错误并且没有修复入口点错误),我尝试过 - 安装Visual C++ express并从头开始.我的老师和我在main()之前检查了这个文件中的所有代码(以及我编写和包含的Stack.h文件中的所有代码),并且没有任何丢失的括号,分号或任何其他标点符号.我不知道还有什么可以尝试的.思考?
c++ program-entry-point compiler-errors entry-point language-lawyer
为什么按照标准不允许a++ = b;
,而c[i++] = d;
允许呢?
(显然,a++ = b;
这是一种不好的风格,但这是一个关于仔细阅读 C 语言标准的问题。)
这是强制性的最小示例:
#include <stdio.h>
int main(void)
{
int a = 10;
int b = 20;
int i = 1;
int c[] = {30, 40};
int d = 50;
a++ = b; // error
c[i++] = d;
printf("%d\n", a); // [21]
printf("%d\n", b); // 20
printf("%d\n", i); // 2
printf("%d\n", c[0]); // 30
printf("%d\n", c[1]); // 50
printf("%d\n", d); // 50
return 0;
}
Run Code Online (Sandbox Code Playgroud)
-std=c90
当使用或进行编译时,GCC …
许多程序都假设sizeof(int)==sizeof(unsigned int)
. IMO 不是,我在标准中找不到任何保证它的内容。我是对还是错?
为什么下面的代码编译为 C++ 时没有任何抱怨,但 C 编译器却抱怨初始化器不是编译时常量?
int x = 2;
int y = 1;
int a[2] = {x, y};
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在cppreference上,我读到了以下内容std::vector<T,Allocator>::operator[]
:
通过此运算符访问不存在的元素是未定义的行为。
标准草案中是否有类似的句子?或者说,我应该从标准的哪一部分推断出这个东西?
我想如果标准对此只字未提,那就会成为 UB。但我还没有找到任何相关信息std::vector<T,Allocator>::at
,所以...
c++ std undefined-behavior language-lawyer indexoutofboundsexception
在 C 中,使用未初始化的变量是未定义的行为。包括:
int x;
int y = x;
Run Code Online (Sandbox Code Playgroud)
但是,假设我有以下内容:
struct Struct {
int a;
int b;
};
struct Struct s;
s.a = 1;
struct Struct s0 = s;
return s; // Assuming an enclosing function with the correct return type
Run Code Online (Sandbox Code Playgroud)
如果某些元素(但不是全部)已被分配,上述代码是否会调用未定义的行为?
是否有一种严格一致的方法来获取 C 中整数的所有位模式,而不依赖于实现定义的行为?
在许多架构上,我可以期望-1
和UINT_MAX
家族给出这样的位模式;这个假设的可移植性如何?
有四个文件,header.h
,A.cpp
,B.cpp
,main.cpp
。
// header.h
struct test {
static inline int get();
};
Run Code Online (Sandbox Code Playgroud)
// a.cpp
#include "header.h"
int testA() { return test::get(); }
int test::get() { return 0; }
Run Code Online (Sandbox Code Playgroud)
// b.cpp
#include "header.h"
int testB() { return test::get(); }
int test::get() { return 1; }
Run Code Online (Sandbox Code Playgroud)
// main.cpp
#include <cstdio>
int testA();
int testB();
int main() {
printf("%d %d\n", testA(), testB());
}
Run Code Online (Sandbox Code Playgroud)
编译时g++ -o main A.cpp B.cpp main.cpp -O0
二进制输出两个相同的数字,而-O3
始终输出0 1 …
language-lawyer ×10
c++ ×6
c ×5
bit ×1
entry-point ×1
integer ×1
lvalue ×1
qt ×1
std ×1
struct ×1