如果地图中只有单个元素,那么std :: map iterator会减少什么返回?这是示例代码
#include <map>
#include <stdio.h>
int main()
{
std::map<int, int> m;
m.insert(std::make_pair(1, 1));
//std::map<int, int>::iterator it = m.begin();
std::map<int, int>::iterator it = m.upper_bound(0);
printf("isbegin: %d\n", it == m.begin());
--it;
bool isend = it == m.end();
printf("isend: %d\n", isend);
}
Run Code Online (Sandbox Code Playgroud)
在Windows上它将打印isend:1,在Linux上使用g ++ 4.6它将打印isend:0.
问题:上面的减量真的是UB的情况吗?如果没有,那么结果是正确的 - Windows还是Linux?
更新:修改代码以显示调用upper_bound
当我这样做:
var m = Regex.Match("aabbccddeeff", "[0-9a-fA-F]{6}");
Run Code Online (Sandbox Code Playgroud)
结果我得到的只有aabbcc.实际上(使用.Matches)有两个匹配:aabbcc和ddeeff.
为什么?这会导致DataAnnotations.RegularExpressionAttribute出现问题,因为它需要覆盖整个输入值的单个匹配.
如何正确编写以获得单个匹配?
我在gcc 4.5.2中遇到-O2问题.说我有这个代码:
//file.cpp
void test::f() {}
//file.h
struct test
{
inline void f();
};
Run Code Online (Sandbox Code Playgroud)
此代码位于共享库中.现在,当我编译没有-O2时,它工作正常.使用-O2时,它表示test :: f()是未定义的符号.显然gcc只是抛弃它,因为它是"内联"(虽然它确实不是).
我的问题是具体的优化标志导致了什么?我的想法是,我想启用-O2但禁用那个确切的标志,以便我可以保持内联不变(这不是我的代码).
我可以只迭代所有这些,但是,这也可以是链接器标志,对吧?这是太多的工作,我只希望有人会有线索.
我有这个Win32代码:
fld x
fmul y
fstsw ax
Run Code Online (Sandbox Code Playgroud)
似乎fmul没有清除FPU状态寄存器位,即如果已经设置了溢出(ax&8),那么它将保持设置fmul.这是正确的行为吗?是否需要在FPU操作之后或之前清除状态?
即我想fmul设置这个位,但它应该重置它吗?我在网上找不到答案.
首先是代码
#include <stdio.h>
typedef wchar_t* BSTR;
wchar_t hello[] = L"Hello";
class _bstr_t {
public:
operator const wchar_t*() const throw() { return hello; }
operator wchar_t*() const throw() { return hello; }
};
class container {
public:
operator _bstr_t() { return _bstr_t(); }
};
int main()
{
// This gives error (with gcc 4.5.2 at least):
// test.cpp:20:27: error: cannot convert "container" to "wchar_t*" in initialization
wchar_t *str = container();
printf("%S\n", str);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是container()可以转换为_bstr_t然后wchar_t* …
这是测试代码:
#include <stdio.h>
struct test
{
int m[1];
};
struct test2: public test
{
int m1[22];
void set(int x, int y) { m[x] = y; }
};
int main()
{
test2 t;
t.m[1] = 123;
t.set(0, 0);
t.set(1, 1);
printf("%d %d\n", t.m[0], t.m[1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我没有编译一次,优化编译一次:
$ g++ -O0 testf.cpp
$ ./a.out
0 1
$ g++ -O2 testf.cpp
$ ./a.out
1 123
Run Code Online (Sandbox Code Playgroud)
在我看来,gcc看到数组大小m [1]并优化对它的访问,始终是第一个元素m [0].问题是:它是优化错误还是,某些C++规则被打破,以便gcc可以做它做的事情,如果是,那么什么规则?
请注意,由于额外的m1 [22]内存(在真实应用程序中设计),不会发生内存/堆栈溢出.我不问这是不是一个好的编程风格,我只是好奇得到上面问题的正确答案.
更新:我接受了std详细信息的答案,但最大的帮助是带有以下链接的注释:"struct hack"在技术上是不确定的行为吗?