将shared_ptr派生类型传递给采用shared_ptr基类型的函数的最佳方法是什么?
我通常通过shared_ptr引用传递s以避免不必要的副本:
int foo(const shared_ptr<bar>& ptr);
Run Code Online (Sandbox Code Playgroud)
但如果我尝试做类似的事情,这不起作用
int foo(const shared_ptr<Base>& ptr);
...
shared_ptr<Derived> bar = make_shared<Derived>();
foo(bar);
Run Code Online (Sandbox Code Playgroud)
我可以用
foo(dynamic_pointer_cast<Base, Derived>(bar));
Run Code Online (Sandbox Code Playgroud)
但这似乎是次优的,原因有两个:
dynamic_cast对于简单的派生到基础演员来说,A 似乎有点过分.dynamic_pointer_cast创建一个指向传递给函数的副本(虽然是临时的).有更好的解决方案吗?
原来这是一个缺少头文件的问题.此外,我在这里尝试做的是一个反模式.通常,
不影响对象生命周期的函数(即对象在函数持续时间内保持有效)应采用普通引用或指针,例如int foo(bar& b).
使用对象的函数(即,是给定对象的最终用户)应该采用unique_ptrby值,例如int foo(unique_ptr<bar> b).std::move调用者应该将值放入函数中.
延长对象生命周期的函数应采用shared_ptrby值,例如int foo(shared_ptr<bar> b).避免循环引用的通常建议适用.
有关详细信息,请参阅Herb Sutter的回归基础讲座.
我正在研究的项目使用多个线程来处理文件集合.每个线程都可以将文件添加到要处理的文件列表中,因此我将(我认为是)一个线程安全的队列放在一起.相关部分如下:
// qMutex is a std::mutex intended to guard the queue
// populatedNotifier is a std::condition_variable intended to
// notify waiting threads of a new item in the queue
void FileQueue::enqueue(std::string&& filename)
{
std::lock_guard<std::mutex> lock(qMutex);
q.push(std::move(filename));
// Notify anyone waiting for additional files that more have arrived
populatedNotifier.notify_one();
}
std::string FileQueue::dequeue(const std::chrono::milliseconds& timeout)
{
std::unique_lock<std::mutex> lock(qMutex);
if (q.empty()) {
if (populatedNotifier.wait_for(lock, timeout) == std::cv_status::no_timeout) {
std::string ret = q.front();
q.pop();
return ret;
}
else {
return std::string();
}
}
else …Run Code Online (Sandbox Code Playgroud) wait_for "阻塞当前线程,直到条件变量被唤醒或在指定的超时持续时间之后"
wait_until "阻止当前线程,直到条件变量被唤醒或达到指定的时间点"
有什么不同?是否会wait_until旋转,以便线程可以在发出信号时完全(或多或少)继续,而wait_for只是将线程添加回调度点?
LocalDateTime在Noda Time中获得系统时间的惯用方法是什么?我能想到的最直接的方法是
var dt = DateTime.Now
LocalDateTime systemTime = new LocalDateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute);
Run Code Online (Sandbox Code Playgroud)
但鉴于Noda Time的全部目的是用具有更好语义的东西替换DateTime,我认为有一种首选方法,而不DateTime是以上述方式使用.我能用Noda的设施做到最好的是
var zone = NodaTime.TimeZones.BclDateTimeZone.ForSystemDefault();
LocalDateTime systemTime = SystemClock.Instance.Now.InZone(zone).LocalDateTime;
Run Code Online (Sandbox Code Playgroud)
但这似乎很冗长.
我正在尝试将OpenCV 2.3.1用于视频捕获和处理应用程序.我从这里下载了预编译的库,然后使用build\x86\vc10\staticlib下载文件夹中的静态库编译测试应用程序.在尝试编译时,Visual Studio使用下面显示的链接器错误来问候我.在过去使用libpng,gzip和其他一些工作之后,我认识到链接器错误中这些库的功能.
使用OpenCV的预编译库是否也需要我链接libpng,libjpeg,gzip和其他许多人,或者我错过了什么?
1>opencv_core231d.lib(persistence.obj) : error LNK2019: unresolved external symbol _gzputs referenced in function "void __cdecl icvPuts(struct CvFileStorage *,char const *)" (?icvPuts@@YAXPAUCvFileStorage@@PBD@Z)
1>opencv_core231d.lib(persistence.obj) : error LNK2019: unresolved external symbol _gzclose referenced in function "void __cdecl icvClose(struct CvFileStorage *)" (?icvClose@@YAXPAUCvFileStorage@@@Z)
1>opencv_core231d.lib(persistence.obj) : error LNK2019: unresolved external symbol _gzopen referenced in function _cvOpenFileStorage
1>opencv_core231d.lib(persistence.obj) : error LNK2019: unresolved external symbol _gzgets referenced in function "char * __cdecl icvGets(struct CvFileStorage *,char *,int)" (?icvGets@@YAPADPAUCvFileStorage@@PADH@Z)
1>opencv_core231d.lib(persistence.obj) : error LNK2019: …Run Code Online (Sandbox Code Playgroud) 我在C++ 11中尝试一个相当简单的正则表达式匹配(使用gcc 4.7.2),但是我遇到了很多麻烦.尝试使用构造模式
std::regex unquoted(R"regex(\s*([^",]+)\s*)regex");
Run Code Online (Sandbox Code Playgroud)
导致构造函数std::regex_error使用代码抛出异常std::regex_constants::error_escape.在线的几个正则表达式测试人员对同一个表达式没有任何问题,我尝试使用不同的一些不同的语法选项无济于事.C++正则表达式语法是否存在根本不同的东西,我没有理解?
我一直在Visual Studio Express 2013中开发一个C#项目,并且遇到了.NET语言的代码契约.他们的简洁性和随附的静态分析工具给我留下了深刻的印象,我开始在我的代码库中使用它们.但是,当我试图运行我的程序时,我遇到了类似于在这个SO线程中发现的错误消息,即
...必须使用代码契约二进制重写器(CCRewrite)重写程序集(可能是"<my project>"),因为它正在调用Contract.Requires并且定义了CONTRACTS_FULL符号.从项目中删除CONTRACTS_FULL符号的任何显式定义并重建...
指南建议要解决此问题,我必须从项目的"属性"页面启用代码约定,但在Express中无法找到代码约定属性条目.
一些MSDN论坛主题似乎表明代码合同的所有工具都包含在Express版本中,但Code Contracts Properties页面却没有.这似乎是这种情况,因为我能够在VSE 2013中运行我的项目,只有在我的大学毕业之前从我的大学获得的Visual Studio 2012 Ultimate的副本启用代码合同之后.
除了通过手动修改项目文件或使用付费版本的Visual Studio修改项目文件外,是否真的无法在Visual Studio Express中使用代码约定?如果是这种情况,我对使用它们非常犹豫,因为我的公司不太可能购买VS许可证.此外,微软试图扩展这种新的优越的验证范例,然后将其仅限于付费用户,这似乎是非常奇怪的.
.net c# code-contracts visual-studio-express visual-studio-2013
如何使用Noda Time获取夏令时的开始和结束日期?下面的功能完成了这个任务,但它非常笨拙,并且正在寻求一个更简单的解决方案.
/// <summary>
/// Gets the start and end of daylight savings time in a given time zone
/// </summary>
/// <param name="tz">The time zone in question</param>
/// <returns>A tuple indicating the start and end of DST</returns>
/// <remarks>Assumes this zone has daylight savings time</remarks>
private Tuple<LocalDateTime, LocalDateTime> GetZoneStartAndEnd(DateTimeZone tz)
{
int thisYear = TimeUtils.SystemLocalDateTime.Year; // Get the year of the current LocalDateTime
// Get January 1, midnight, of this year and next year.
var yearStart = new LocalDateTime(thisYear, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Json.NET序列化一些Noda Time值并遇到麻烦.序列化很简单:
LocalDateTime dt = ... // Assigned elsewhere
LocalDateTimePattern isoDateTimePattern = LocalDateTimePattern.GeneralIsoPattern;
JObject root = new JObject();
root.Add("time", isoDateTimePattern.Format(dt));
// Serialize other elements
using (var sw = new StreamWriter(stream)) {
serializer.Serialize(sw, root);
}
Run Code Online (Sandbox Code Playgroud)
但是反序列化是有问题的.Json.NET似乎从上面识别出ISO格式的日期和时间,并自动将其转换为DateTime对象,这不是我想要的.
using (var sr = new StreamReader(stream)) {
using (var jr = new JsonTextReader(sr)) {
var root = serializer.Deserialize<JObject>(jr);
// Deserialize other elements
var time = root.GetValue("time"); // time.Type is JTokenType.Date
string timeStr = time.Value<string>(); // Is "01/10/2014 10:37:32"
// Kaboom. String is not in the …Run Code Online (Sandbox Code Playgroud) 我知道a的默认值std::chrono::system_clock::time_point是时钟的纪元,但是我在C++ 11标准中找不到任何system_clock与epix纪元相同的命令(1970-01-01T00:00: 00Z).在Linux和Windows上假设这种情况是否安全?或者使用它会更聪明std::chrono::system_clock::from_time_t(0)吗?