对于C ++ 11,C ++在标准中具有一些计时功能。这些功能之一是时钟的标准接口,该接口基本上允许在调用now时钟功能时获取时间。
到现在为止一切都很好,但是我看不到要求 now成为静态函数的原因。在托管系统上,标准时钟可能完全可以通过系统调用或通过读取处理器计数器等来实现。但是,这限制了需要维持某些状态的自定义时钟的实现。使用此接口,要么无法实现某些时钟,要么必须使用全局状态。
我遇到的一个问题基本上是将本地时钟与从NTP服务器获得的时间同步。代码看起来像这样:
class sntp_clock
{
public:
sntp_clock()
: local_time_at_ctor(read_some_cpu_counter())
, sntp_time_at_ctor(read_sntp_time()) {}
sntp_time_t now() const {
return sntp_time_at_ctor + (read_some_cpu_counter() - local_time_at_ctor);
}
/* required types etc */
private:
local_time_t local_time_at_ctor;
sntp_time_t sntp_time_at_ctor;
};
Run Code Online (Sandbox Code Playgroud)
由于不能now将状态也设置为静态,所以不能使其静态,因此此时钟不满足C ++标准中Clock的要求。
另外,出于效率的考虑,我可能不想启动一个时钟实例存在的cpu计数器,但是又一次,由于它now是静态的,因此我无法确切知道何时开始计时或何时停止计时。
我的问题是为什么时钟有静态now要求?
注意:当前的标准草稿必须now是静态的:http : //eel.is/c++draft/time.clock.req#tab : time.clock
Boost.Chrono文档具有相同的要求:https ://www.boost.org/doc/libs/1_63_0/doc/html/chrono/reference.html#chrono.reference.cpp0x.clock
有没有办法将整数模板参数限制为专门化而不是代码冗余?
// redundant code
template <int N>
struct A {};
template <>
struct A <0> {};
template <>
struct A <1> {};
// what i want is some thing like this
template <int N>
struct A {};
template <>
struct A <N < 2> {};
Run Code Online (Sandbox Code Playgroud) 我正在开发一个圆形菜单应用程序.
在我使用位图作为背景之前,我必须将其裁剪为圆形.
好吧,我找到了一个代码片段,用于在图像或位图中裁剪图片C#裁剪圆圈或:
public System.Drawing.Image x(string sourceFile, int circleUpperLeftX, int circleUpperLeftY, int circleDiameter)
{
Bitmap SourceImage = new Bitmap(System.Drawing.Image.FromFile(sourceFile));
Rectangle CropRect = new Rectangle(circleUpperLeftX, circleUpperLeftY, circleDiameter, circleDiameter);
Bitmap CroppedImage = SourceImage.Clone(CropRect, SourceImage.PixelFormat);
TextureBrush TB = new TextureBrush(CroppedImage);
Bitmap FinalImage = new Bitmap(circleDiameter, circleDiameter);
Graphics G = Graphics.FromImage(FinalImage);
G.FillEllipse(TB, 0, 0, circleDiameter, circleDiameter);
return FinalImage;
}
Run Code Online (Sandbox Code Playgroud)
但是在第6行中,代码会导致内存泄漏一段时间.
好吧,我尝试添加TB.Dispose(); 但是,这没有帮助.
我该怎么办?
我正在研究点对点网络应用程序,但我无法解决我将如何在UDP套接字上提供安全性.
我不想重新发明轮子,但我不应该选择哪种方法来实现安全性.
我的想法是在每个对等体之间生成RSA密钥,并首先通过不安全的套接字共享这些密钥,并使这些密钥保持连接安全.但我不确定如何实施RSA以及这是否是最安全的方法.
顺便说一下,我正在为这个项目使用C++
非常感谢你
在C#中是否有相同的功能?
#define TypeConstant int
int main()
{
TypeConstant x = 5;
}
Run Code Online (Sandbox Code Playgroud)
非常感谢你!
编辑:我不确定这与定义常量常量有什么关系,我已明确写出类型常量,而不是常量值!在你投票之前阅读!
c++ ×3
c# ×2
c++-chrono ×1
constants ×1
gdi+ ×1
memory-leaks ×1
security ×1
sockets ×1
templates ×1