什么是"序列点"?
未定义的行为和序列点之间的关系是什么?
我经常使用有趣和复杂的表达方式a[++i] = i;,让自己感觉更好.我为什么要停止使用它们?
如果您已阅读此内容,请务必访问后续问题重新加载未定义的行为和序列点.
(注意:这是Stack Overflow的C++常见问题解答的一个条目.如果你想批评在这种形式下提供常见问题解答的想法,那么发布所有这些的元数据的发布将是这样做的地方.这个问题在C++聊天室中受到监控,其中FAQ的想法一开始就出现了,所以你的答案很可能被那些提出想法的人阅读.)
在Javascript中,您可以++在变量名称之前或之后使用运算符.这些增量变量的方法之间的区别是什么?
我认为首先从0开始400*400=160000转换为28928并以循环方式转换为160000时间为int类型(比如sizeof(int)= 2字节),假设它如下:
然后将28928除以400,其中得到72,结果随变量的类型而变化.我的假设是正确的还是有其他解释?
int& foo() {
printf("Foo\n");
static int a;
return a;
}
int bar() {
printf("Bar\n");
return 1;
}
void main() {
foo() = bar();
}
Run Code Online (Sandbox Code Playgroud)
我不确定应该首先评估哪一个.
我在VC中尝试过首先执行bar函数.但是,在g ++编译器(FreeBSD)中,它首先给出了foo函数的评估.
很多有趣的问题来自上面的问题,假设我有一个动态数组(std :: vector)
std::vector<int> vec;
int foobar() {
vec.resize( vec.size() + 1 );
return vec.size();
}
void main() {
vec.resize( 2 );
vec[0] = foobar();
}
Run Code Online (Sandbox Code Playgroud)
根据以前的结果,vc计算foobar(),然后执行向量运算符[].在这种情况下没有问题.但是,对于gcc,由于正在评估vec [0]并且foobar()函数可能导致更改数组的内部指针.执行foobar()后,vec [0]可以无效.
是否意味着我们需要将代码分开
void main() {
vec.resize( 2 );
int a = foobar();
vec[0] = a;
}
Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
class MyClass
{
...
};
template <typename Object>
class List
{
public:
void insert(const Object & x)
{
// call when Object is MyClass
}
void insert(const Object & x)
{
// call when Object is MyClass*
}
}
int main()
{
MyClass a;
List<MyClass> lst;
List<MyClass*> plst;
lst.insert(a);
plst.insert(new Myclass);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何根据模板是类还是指针来告诉编译器调用不同的方法?
如何修复上面的代码?
这就是我今天编写的内容
#include <iostream>
using namespace std;
int function1()
{
cout<<"hello from function1()"; return 0;
}
int function2()
{
cout<<"hello from function2()"; return 0;
}
int main()
{
int func_diffresult = 0;
func_diffresult = function1() - function2();
cout<<func_diffresult; /** prints 0 correctly **/
}
Run Code Online (Sandbox Code Playgroud)
输出是得到的hello from function2()hello from function1().我认为输出应该是hello from function1()hello from function2().我的编译器和我一起玩吗?
换句话说就是
d = {}
d["key"] = len(d)
Run Code Online (Sandbox Code Playgroud)
安全的Python?
我知道这是C++中未定义的行为 ; 在计算要分配给它的值之前,程序可能会获得对元素的引用.这在Python中是类似的还是len(d)以前总是计算d.__getitem__("key")?
C99引入了结构指定的初始化器的概念.例如,给定:
typedef struct {
int c;
char a;
float b;
} X;
Run Code Online (Sandbox Code Playgroud)
我可以初始化为:X foo = {.a = '\1', .b = 2.0F, .c = 4};并且调用:printf("c = %d\na = %hhu\nb = %f", foo.c, foo.a, foo.b);将输出:
c = 4
a = 1
b = 2.000000
这里提到这有分配给的"令人惊讶的行为" c,然后a再b独立我指定的初始的顺序.
如果我有这样的函数,这将成为一个真正的问题:
int i = 0;
int f() {
return ++i;
}
int g() {
i += 2;
return i;
}
int h() {
i += 4; …Run Code Online (Sandbox Code Playgroud) 表达式的右侧是先评估还是左侧评估?
void main ()
{
int i = 0 , a[3] ;
a[i] = i++;
printf ("%d",a[i]) ;
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
什么属于教育工具,以证明人们在C/C++中做出的无根据的假设?
在C99中,f()+ g()未定义或仅仅未指定?
在语句中,"function1()+ function2();"将首先调用哪个函数?
c++ ×6
c ×4
c++-faq ×2
evaluation ×2
function ×2
increment ×1
javascript ×1
python ×1
sfinae ×1
templates ×1
types ×1
undefined ×1
unspecified ×1
warnings ×1