我正在使用VC++ 11(CTP Update 1)编译x86目标时遇到随机浮点错误.请参阅下面的简短示例"test.cpp",并使用以下命令进行编译:
cl /GL /O2 /EHsc test.cpp /link /MACHINE:X86
Run Code Online (Sandbox Code Playgroud)
输出应该是10 == 10
,但它会10 == 0
在/GL
(整个程序优化)启用时产生.问题似乎是get_scaling_factor()
将结果推送到浮点堆栈,但调用函数期望它在SSE寄存器XMM0中.
问题:我错过了一些明显的东西,还是这真的是一个错误?当然,测试程序没有意义,因为它是一个简化的测试用例.
TEST.CPP:
#include <iostream>
template <typename T>
inline T get_scaling_factor(int units)
{
switch (units)
{
case 0: return 1;
case 1: return 10;
case 2: return 100;
case 3: return 1000;
case 4: return 10000;
case 5: return 100000;
case 6: return 1000000;
case 7: return 10000000;
case 8: return 100000000; …
Run Code Online (Sandbox Code Playgroud) c++ visual-c++ compiler-bug visual-studio-2012 visual-c++-2012
我在C#中有一个函数,我想将它(在其他一些东西中)移植到F#,只是为了做到这一点.不幸的是,我只是遇到了一个似乎没有办法在F#中表达这种情况的案例:拿这个C#函数
public static T Min<T>(params T[] p) where T : IComparable
{
T m1 = p[0];
foreach (T v in p)
{
m1 = (m1.CompareTo(v) < 0) ? m1 : v;
}
return m1;
}
Run Code Online (Sandbox Code Playgroud)
我认为这很容易,但我不明白如何在F#中指定变量参数列表.我试过这个:
let rec Min l =
match l with
| [] -> 0 // should throw exception here
| [v] -> v
| (h::t) -> min h (Min t)
Run Code Online (Sandbox Code Playgroud)
但是从C#调用它需要一个Microsoft.FSharp.Collections.List
.有可能让它期待一个params T[]
,如果是这样,怎么样?
为什么这样做:
std::pair<int, int> p = {1,2};
std::vector<std::pair<int, int>> vp = { {1,2}, {3,4} };
Run Code Online (Sandbox Code Playgroud)
但这不是吗?
std::array<int, 2> a = {1,2}; // still ok
std::vector<std::array<int, 2>> va = { {1,2}, {3,4} };
Run Code Online (Sandbox Code Playgroud)
使用g ++ 4.5.1 -std=c++0x
,第二行失败:
错误:无法转换
‘{{1, 2}, {3, 4}}’
为‘std::vector<std::array<int, 2u> >’
谢谢
选项"--address-model = 32,64"是否可以构建32和64个库,还是必须进行两个单独的构建?
我有一个带有原子成员变量的类:
struct Foo
{
std::atomic<bool> bar;
/* ... lots of other stuff, not relevant here ... */
Foo()
: bar( false )
{}
/* Trivial implementation fails in gcc 4.7 with:
* error: use of deleted function ‘std::atomic<bool>::atomic(const td::atomic<bool>&)’
*/
Foo( Foo&& other )
: bar( other.bar )
{}
};
Foo f;
Foo f2(std::move(f)); // use the move
Run Code Online (Sandbox Code Playgroud)
移动构造函数应该怎么样?
GCC 4.7不喜欢我的任何企图(如添加std::move()
周围other.bar
)和净是出奇的安静这里...
在C++ 11标准中,机器模型从单线程机器变为多线程机器.
这是否意味着static int x; void func() { x = 0; while (x == 0) {} }
优化输出读取的典型示例将不再发生在C++ 11中?
编辑:对于那些不知道这个例子的人(我非常惊讶),请阅读:https://en.wikipedia.org/wiki/Volatile_variable
EDIT2:好的,我真的很期待所有知道volatile
这个例子的人.
如果您使用示例中的代码,则循环中读取的变量将被优化,使循环无限.
解决方案当然是使用volatile
它会强制编译器在每次访问时读取变量.
我的问题是,如果这是C++ 11中不推荐使用的问题,因为机器模型是多线程的,因此编译器应该考虑对系统中存在的变量的并发访问.
所以,用下面的代码,改变参数的类型x
从const ull
到const ull&
(有typedef unsigned long long ull
)在约25%的加速效果,当用gcc 4.7.2和标志编译-O3 -std=c++11 -g
,我想不通为什么会做出如此大的差异.
static void inline single_mult(const std::vector<ull>::iterator& data,
const std::vector<ull>::const_iterator& rbegin,
const std::vector<ull>::const_iterator& rend,
const ull x) {
ull tmp=0, carry=0, i=0;
for (auto rhs_it = rbegin; rhs_it != rend; ++rhs_it) {
tmp = x*(*rhs_it) + data[i] + carry;
if (tmp >= imax) {
carry = tmp >> numbits;
tmp &= imax - 1;
} else {
carry = 0;
}
data[i++] …
Run Code Online (Sandbox Code Playgroud) 假设我有以下代码我想重构:
int toFuture()
{
precalc();
int calc = 5 * foobar_x() + 3;
postcalc();
return calc;
}
int toPast()
{
precalc();
int calc = 5 * foobar_y() - 9;
postcalc();
return calc;
}
Run Code Online (Sandbox Code Playgroud)
在classic-C中,我将这个代码重构为一个worker(),它接受一个执行计算的函数指针:worker()中的公共代码,函数指针提供的特定代码.
使用C++ 11,我应该使用lambda吗?如果是这样,在这种情况下我将如何实现它?
编辑:我只是想到一个模板也可以工作.模板实现如何与其他两个进行比较?
我正在尝试在C++ 11中实现一个带有lambda函数的映射
std::map<int, int, [](const int&a, const int& b) { return a < b; }> test;
Run Code Online (Sandbox Code Playgroud)
但那失败了
错误:模板参数列表中参数3的类型/值不匹配
‘template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map’
错误:期望一个类型,得到
‘{}’
错误:
‘;’
令牌之前的声明中的无效类型
有什么建议?
在成员函数中编写lambda函数时,有没有办法按值捕获封闭类的字段?默认的catch-all =
不起作用,因为当我在lambda中引用变量时,我得到的是从捕获的this指针中取消引用,以及在捕获列表中显式命名变量,因为我得到两个编译错误:capture of non-variable <name>
,和‘this’ was not captured for this lambda function
c++ ×9
c++11 ×7
lambda ×3
arrays ×1
atomic ×1
bjam ×1
boost ×1
c# ×1
closures ×1
compiler-bug ×1
f# ×1
gcc ×1
libstdc++ ×1
optimization ×1
performance ×1
visual-c++ ×1
volatile ×1