草庐IT

duration_cast

全部标签

c# - InvalidCastException : Unable To Cast Objects of type [base] to type [subclass]

我有一个继承自MembershipUser的自定义CustomMembershipUser。publicclassConfigMembershipUser:MembershipUser{//customstuff}我正在使用Linq-to-SQL从数据库中读取并获取用户实体;为了使此功能成为MembershipUser,我定义了一个显式转换:publicstaticexplicitoperatorMembershipUser(Useruser){DateTimenow=DateTime.Now;if(user==null)returnnull;returnnewMembershipUs

kotlin - 处理这种情况的最佳方法是 "smart cast is imposible"

我想知道处理这种情况的最佳方法是什么classPerson(varname:String?=null,varage:Int?=null){funtest(){if(name!=null&&age!=null)doSth(name,age)//smartcastimposible}fundoSth(someValue:String,someValue2:Int){}}调用doSth方法并确保name和age为ntnull的最简单方法是什么?我正在寻找一些简单的东西,比如我会简单地使用let的可变场景name?.let{doSth(it)} 最佳答案

c++ - 为什么我需要从 `long &` 到 `int &` 的 reinterpret_cast,两者都是 32 位 (Windows LLP64)?

我在跨平台环境中使用Qt。我们遇到了以下问题:在Windows上,int和longint都是32位整数;在64位MacOS和Linux上,int是32位的,longint是64位的(参见https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models)。因此,跨平台库倾向于提供它们自己的固定位类型定义。在Windows上,Qt将quint32定义为unsignedint并且不使用unsignedlong整数。另一个库将其Uint32定义为unsignedlong。因此,两者实际上都是32位无符号整数,但具有不同的原始数据

c++ - 如何很好地将 "cast"qint64 转换为 QProgressBar 的 int

我正在使用QFtp(是的..我知道)并且一切正常。使用他们自己示例中的代码作为指导。http://doc.qt.io/archives/qt-4.7/network-qftp-ftpwindow-cpp.html我遇到的唯一问题是在发送(或接收)大文件(比如3GB)时进度条出现故障。这是由于从qint64到int的转换:voidFtpWindow::updateDataTransferProgress(qint64readBytes,qint64totalBytes){progressDialog->setMaximum(totalBytes);progressDialog->setV

C++ 是否 reinterpret_cast 总是返回结果?

我有两个类,A和B。A是B的父类,我有一个函数接收指向A类型类的指针,检查它是否也是B类型,如果是将调用另一个函数,该函数接受一个指向类型B的类的指针。当函数调用另一个函数时,我提供reinterpret_cast(a)作为参数。如果这看起来模棱两可,这里有一个代码示例:voidabc(A*a){if(a->IsA("B")){//pleasedontworrymuchaboutthisline,//myrealconcernisthereinterpret_castdef(reinterpret_cast(a));};};现在您知道我是如何调用“def”的了,我想知道reinterp

c++ - 如何避免在 C++ 中将 const_cast 与 std::vector::erase() 一起使用?

我有这样一个类:templateclassAdjacencyList{public:voiddelete_node(constT&);protected:consttypenamestd::vector::const_iterator_iterator_for_node(conststd::vector&,constT&);};templatevoidAdjacencyList::delete_node(constT&node){_nodes.erase(_iterator_for_node(_nodes,node));}templateconsttypenamestd::vector

c++ - 使用默认值而不是异常来 boost numeric_cast<>?

每当boost的numeric_cast转换失败,抛出异常。boost中是否有类似的模板让我指定一个默认值,或者在这种情况下捕获异常是我唯一能做的?我不太担心所有额外异常处理的性能,但我宁愿使用标准模板也不愿编写无用的包装函数。另外,根据以往的经验,我认为boost很有可能真的有我想的,只是一直没有找到。 最佳答案 numeric_cast函数只是调用boost::numeric::converter具有默认参数的模板类。其中一个参数是OverflowHandler,默认值为def_overflow_handler,但您可以指定si

C++ 编译错误 : "cast from ' WCHAR *' to ' WORD' loses precision"

MyGUI库。源码中有一行:mHandle=(size_t)::LoadCursor(NULL,MAKEINTRESOURCE(IDC_ARROW));mHandle是size_tLoadCursor返回HCURSOR。错误:D:\Dev\MyGUI_3.2.0_RC1\Common\Input\Win32\ResourceW32Pointer.cpp:48:error:castfrom'WCHAR*'to'WORD'losesprecision这是完整的来源:www.pastebin.com/gzqLBFh9MinGW编译器。有错误castfrom'CHAR*'to'WORD'los

c++ - constant 和 reinterpret cast 是否在编译时发生?

我读到过static_cast发生在编译时,dynamic_cast发生在运行时,因此比static_cast慢。dynamic_cast可以返回空指针(当与指针一起使用时)或以其他方式抛出错误的转换异常。我的问题是reinterpret_cast和const_cast是发生在编译时还是运行时?我认为解释转换发生在运行时,因为它的行为类似于dynamic_cast指示转换是否成功。我对么?const_cast是编译时间吗? 最佳答案 动态转换是唯一需要在运行时“计算”的。所有其他类型转换均在编译时计算。static_cast的机器代

c++ - unsigned char 的 boost::lexical_cast 和 std::to_string 的正确结果是什么

以下从字符转换为字符串的正确结果是什么?我听说旧的bo​​ost版本1.46lexical_cast输出是56,我附近没有那个版本,我无法测试它。但boostlibrary(1.49)输出为:8unsignedcharc=56;std::strings=boost::lexical_cast(c);std::coutC++11to_string输出为:56std::cout 最佳答案 std::to_string仅提供数字类型的重载,可能解析为unsigned在这种情况下的版本。lexical_cast,OTOH,依赖std::os