编辑说明:最初的问题说非法,现在说未指定。
感谢最近 Jason Turner 视频的视频评论部分,我了解到这std::complex<int>是未指定的。
但所有(AFAIK)实现似乎都可以愉快地编译
std::complex<int>
Run Code Online (Sandbox Code Playgroud)
但有些功能如std::abs()已被破坏std::complex<int>,因此它实际上在那些主流实现中不可用。
我想知道是否有某种原因导致从未实现“不良”类型的检测。我知道 std lib 实现需要与较旧的标准一起工作,因此它们不能只是std::floating_point在各处停留概念,但即使在 C++20 之前,我们也有方法来约束模板。
换句话说:这只是“很好,但我们没有时间”问题,还是存在某些兼容性原因来保持此编译。我唯一能想到的是,有些人正在使用 std::complex 和 std lib“制造商”不想明显破坏他们已经损坏的代码。
c++ complex-numbers language-lawyer unspecified-behavior c++20
我有一个任务,每隔"圆"分钟做一些事情(在xx:xx:00)我使用类似的东西
const int statisticsInterval=60;
time_t t=0;
while (1)
{
if (abs(t-time(NULL)==0)) //to avoid multiple calls in the same second that is the multiple of 60
boost::this_thread::sleep(boost::posix_time::seconds(2));//2, not 1 to make sure that 1 second passes
t=time(NULL);
boost::this_thread::sleep(boost::posix_time::seconds(statisticsInterval-(t%statisticsInterval)));
//DO WORK
}
Run Code Online (Sandbox Code Playgroud)
如你所见,我使用睡眠(60秒 - 当前分钟经过的秒数).但是一位程序员告诉我,这不准确,我应该把它改成while while while while sleep(1).我认为他是对的非常怀疑,但我只是想检查是否有人知道如果睡眠间隔很长,精度是否较低.我假设睡眠是以一种方式实现的,即在将来触发器被激活并且线程被置于"准备执行线程组"的某个时间,所以我认为没有理由精确地使用diff.BTW OS是ubuntu,我不关心不到2-3秒的错误.例如,如果我睡了52秒,53.8睡眠是完全可以接受的.PS我知道睡眠定义的最短时间,理论上我的线程可能会在2047年被激活.但我问的是现实场景.
简短的问题:有没有更短的方法来做到这一点
array<array<atomic<int>,n>,m> matrix;
Run Code Online (Sandbox Code Playgroud)
我希望有类似的东西
array< atomic< int>,n,m> matrix;
Run Code Online (Sandbox Code Playgroud)
但它不起作用......
C++没有办法获取枚举的字符串表示.人们解决这个问题通过编写又名含有大量的样板代码自定义函数
switch与case XYZ return "XYZ";
那当然要求枚举的用户知道自定义函数的名称.
所以我想我可以添加一个专门化std::to_string来让用户to_string在我的枚举上使用.像这样的东西:
//
#include <iostream>
#include <string>
#include <cassert>
#define TEST
class Car
{
public:
enum class Color
{
Red,
Blue,
White
};
};
#ifdef TEST
#include <string>
namespace std
{
std::string to_string (Car::Color c)
{
switch (c)
{
case Car::Color::Red:
return "Red";
case Car::Color::Blue:
return "Blue";
case Car::Color::White:
return "White";
default:
{
assert(0);
return "";
}
}
}
}
#endif
int main()
{
std::cout << std::to_string(Car::Color::White) << std::endl; …Run Code Online (Sandbox Code Playgroud) 这主要是一种单线型问题,出于可读性原因,我通常会将这些代码写成多行.
所以我的问题是我可以在定义它的同一语句中调用递归lambda吗?
所以不是这样的:
int n=3;
function<void(int)> f {[n,&f](int i){if (i>1) { cout << "func(a, "; f(i-1); cout << ")";} else cout << "a";}};
f(n);
Run Code Online (Sandbox Code Playgroud)
n在定义f的同一行中调用该函数.
我想知道在某个执行点分配的某些对象的确切实例数.主要是为了寻找可能的内存泄漏(我主要使用RAII,几乎没有新的,但在添加新元素或类似的东西之前,我仍然可以在向量上忘记.clear()).我可以有一个
atomic<int> cntMyObject;
Run Code Online (Sandbox Code Playgroud)
我 - 在析构函数中,++增加构造函数,cpy构造函数(我希望我涵盖了一切:)).但这对每个班级来说都是硬编码.在"释放"模式下禁用它并不简单.那么有没有简单优雅的方法可以轻松禁用来计算对象实例?
template <class F, class... Args>
void for_each_argument(F f, Args&&... args) {
[](...){}((f(std::forward<Args>(args)), 0)...);
}
Run Code Online (Sandbox Code Playgroud)
它最近在isocpp.org上有特色,没有任何解释.
谷歌严格别名的第一个结果之一就是这篇文章
http://dbp-consulting.com/tutorials/StrictAliasing.html
我注意到的一个有趣的事情是:http://goo.gl/lPtIa5
uint32_t swaphalves(uint32_t a) {
uint32_t acopy = a;
uint16_t* ptr = (uint16_t*)&acopy;
uint16_t tmp = ptr[0];
ptr[0] = ptr[1];
ptr[1] = tmp;
return acopy;
}
Run Code Online (Sandbox Code Playgroud)
被编译为
swaphalves(unsigned int):
mov eax, edi
ret
Run Code Online (Sandbox Code Playgroud)
由GCC 4.4.7.任何比这更新的编译器(文章中提到的4.4所以文章没有错)都没有实现该功能,因为它可以使用严格别名.这是什么原因?它实际上是GCC中的错误还是GCC决定放弃它,因为许多行代码是以产生UB的方式编写的,或者它只是一个持续多年的编译器回归...而Clang也没有优化它.
这主要是语言律师的问题,我怀疑大多数实现都会麻烦,特别是因为这可能会增加每个用户的编译时间。
话虽如此:如果对std :: set的某些实现是针对每个实例和共享的256个值的静态数组使用位集实现的(由于键为const,这是安全的),根据该标准,这是否合法?