我正在绘制几个从窗口高度和宽度键入的形状(例如圆圈).由于窗口始终以给定大小开始,因此它们被正确绘制,但是当窗口调整大小时,它会使宽高比混乱.
无论窗口大小如何,我如何正确绘制形状?
我正在尝试生成的程序集,发现了一件有趣的事情.有两个函数执行相同的计算.它们之间的唯一区别在于结果总和的方式.
#include <cmath>
double func1(double x, double y)
{
double result1;
double result2;
if (x*x < 0.0) result1 = 0.0;
else
{
result1 = x*x+x+y;
}
if (y*y < 0.0) result2 = 0.0;
else
{
result2 = y*y+y+x;
}
return (result1 + result2) * 40.0;
}
double func2(double x, double y)
{
double result = 0.0;
if (x*x >= 0.0)
{
result += x*x+x+y;
}
if (y*y >= 0.0)
{
result += y*y+y+x;
}
return result * 40.0;
}
Run Code Online (Sandbox Code Playgroud)
由x86 …
我从AGGREGATE Magic中找到了一个快速计算最大值的技巧.这是整数的唯一问题,但是我尝试了一些东西,不知道如何为无符号整数制作一个版本.
inline int32_t max(int32_t a, int32_t b)
{
return a - ((a-b) & (a-b)>>31);
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?
编辑
不要使用它,因为正如其他人所说,它会产生不确定的行为.对于任何现代架构,编译器都能够从中发出无分支条件移动指令return (a > b) ? a : b,这将比所讨论的函数更快.
C++20 是否允许衰减为函数指针的非捕获 lambda 直接作为非类型模板参数传递?如果是这样,正确的语法是什么?
我已经在各种版本的 clang 和 gcc 中使用-std=c++2a.
#include <iostream>
template<auto f>
struct S {
static void invoke(int x) { f(x); }
};
using X = S<+[](int x) -> void { std::cout << x << " hello\n"; }>;
int main()
{
X::invoke(42);
}
Run Code Online (Sandbox Code Playgroud)
gcc 毫无怨言地编译代码,代码按预期运行。
clang 编译失败并出现以下错误:
error: a lambda expression cannot appear in this context
using X = S<+[](int x) -> void { std::cout << x << " hello\n"; }>;
^
Run Code Online (Sandbox Code Playgroud)
这是完整的代码(在线版本):
Clang 10.0.0 头:https …
我正面临一个问题,我正在尝试使用特定类型的参数包创建一个可变成员函数.
template <typename T>
struct A
{
using result_type = T;
T operator()(T a, T b)
{
return a+b;
}
};
template <typename Functor>
struct B
{
using T = typename Functor::result_type;
T operator()(Functor &&f, T... args)
{
return f(args...);
}
};
Run Code Online (Sandbox Code Playgroud)
它应该像以下一样工作:
A<int> a;
B<A<int>> b;
int result = b(a, 2, 3); // should return 5
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
error: type 'T' (aka 'typename Functor::result_type') of function parameter pack does not contain any unexpanded parameter packs
T operator()(Functor &&f, T... args) …Run Code Online (Sandbox Code Playgroud) 我在Visual Studio 2015中面临一个非常奇怪的错误消息.以下删除了代码:
struct A
{
A(int val = 0)
:
x(val)
{}
int x = 0;
};
struct B: A
{
static int y;
};
int B::y = 1;
struct C: B
{
};
int main()
{
C c;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在Clang上编译没有任何问题.但Visual Studio 2015 IntelliSense提供以下错误消息:
the default constructor of "C" cannot be referenced -- it is a deleted function
Run Code Online (Sandbox Code Playgroud)
我在代码中遗漏了什么,或者这是Visual Studio中的错误?
UPDATE
根据这里的评论和答案,我打开了一个关于Microsoft Connect 的错误报告.
MSVC多年来一直支持AVX/AVX2指令,根据这篇msdn博客文章,它可以自动生成融合乘法加法(FMA)指令.
然而,以下两个函数都没有编译为FMA指令:
float func1(float x, float y, float z)
{
return x * y + z;
}
float func2(float x, float y, float z)
{
return std::fma(x,y,z);
}
Run Code Online (Sandbox Code Playgroud)
更糟糕的是,std :: fma没有实现为单个FMA指令,它执行速度非常快,比平原慢得多x * y + z(如果实现不依赖于FMA指令,则预期std :: fma的性能很差).
我用/arch:AVX2 /O2 /Qvec旗帜编译.也尝试过/fp:fast,没有成功.
所以问题是MSVC如何被迫自动发出FMA指令?
UPDATE
有一个#pragma fp_contract (on|off),(看起来像)什么都不做.
我正在尝试使用模板元编程来进行N维嵌套的metaloops.嵌套部分是微不足道的,但是将所有任意数量的迭代索引作为模板参数传递到最内部循环似乎是有问题的.
一个简单的unnested metaloop看起来像:
template <size_t I, size_t N>
struct meta_for
{
template <typename Lambda>
inline meta_for(Lambda &&iteration)
{
iteration(I);
meta_for<I+1, N> next(static_cast<Lambda&&>(iteration));
}
};
template <size_t N>
struct meta_for<N, N>
{
template <typename Lambda>
inline meta_for(Lambda &&iteration)
{
return;
}
};
#include <iostream>
int main()
{
meta_for<0, 10>([&](size_t i) // perform 10 iterations
{
std::cout << i << '\n';
});
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在,我想创建一个metaloop,它接受表示维度(嵌套级别)的N参数,使用如下:
#include <iostream>
int main()
{
// perform 3 dimensionally nested iterations
// each index goes from …Run Code Online (Sandbox Code Playgroud) c++ templates metaprogramming template-meta-programming c++11
这是我刚才想到的一个有趣的问题.给定struct底层聚合:
#include <array>
template <typename T, size_t N>
struct A
{
constexpr A() = default;
template <typename ... Ts>
constexpr A(const T& value, const Ts& ... values); // magic
std::array<T, N> arr; // aggregate
};
Run Code Online (Sandbox Code Playgroud)
你将如何实现可变参数模板的构造A(const T& value, const Ts& ... values)来
T和另一个的值A<T, N>满足上述要求,可以执行以下操作:
int main()
{
A<int, 3> x(1, 2, 3);
A<int, 2> y(1, 2);
A<int, 6> a(x, 1, 2, 3);
A<int, 6> b(1, x, 2, …Run Code Online (Sandbox Code Playgroud) 如果我想将我的default-ed默认构造函数声明为,那么我会得到Clang 3.8和GCC 5.3的编译器错误constexpr.根据这个 stackoverflow问题,它应该工作正常:
struct A
{
constexpr A() = default;
int x;
};
Run Code Online (Sandbox Code Playgroud)
然而:
Error: defaulted definition of default constructor is not constexpr
Run Code Online (Sandbox Code Playgroud)
你有什么线索到底是什么吗?