草庐IT

static-cast

全部标签

c++ - boost::lexical_cast<std::string>(Int_Type) 可以抛出吗?

有没有可能boost::lexical_cast(Int_Type)扔?我唯一能想到的是字符串没有内存的地方,但是还有其他更合理的选择吗? 最佳答案 根据documentation,lexical_cast可以扔bad_lexical_cast.最重要的是,正如您已经提到的,可能存在动态分配,它总是会导致bad_alloc异常。编辑:至于具体情况lexical_cast,除了分配错误之外,链上的任何部分似乎都不太可能失败,但文档并不(据我所知)保证不会出现“错误转换”异常。 关于c++-

c++ - 实现自定义(字符串)流时的 Xcode 4.5.2 libc++ std::bad_cast

我在使用char16_t作为char类型实现我自己的自定义stringbuf和stringstream时遇到问题。作为测试,我使用了一个nullstringbuf和nullstringstream,它们是我在一本旧的、可能已经过时的C++手册中找到的。这个nullstringstream充当“/dev/null”并且是一个简单的实现。当我使用时,这个简单的nullstream正在工作但是当我使用时失败并出现std::bad_cast异常.我知道必须有一个char_traits可用,但libc++似乎有它。classnullstringbuf:publicstd::basic_strin

C++ lambda : Access static method in lambda leads to error 'this was not captured for this lambda function'

考虑以下代码://thisiswhatIwanttocall;Icannotmodifyitssignaturevoidsome_library_method(void(*fp)(void));classSingleton{public:staticSingleton*instance();voidfoo();voidbar();private:Singleton();};voidSingleton::foo(){//thisleadstoanerror('this'wasnotcapturedforthislambdafunction)void(*func_pointer)(void

c++ - 写static const uint变量和匿名枚举变量有什么区别?

正在查看boostasiossl_client.cppexample并在顶部找到了这个:enum{max_length=1024};想知道,这个和有什么区别吗namespace{constintmax_length=1024;}或staticconstintmax_length=1024;或者也许它们是绝对相等的,但这只是更短? 最佳答案 如果您将它用作值而不是引用,它们是等价的。enum{constantname=initializer};习语曾经在头文件中非常流行,因此您可以在类声明中毫无问题地使用它:structX{enum{

c++ - header 中的 private static const 成员变量与 cpp 中的 const 变量

为什么我应该在header中声明一个私有(private)的staticconst变量(并在cpp中初始化它)而不是仅仅在cpp中定义+声明它?即案例1.hclassMyClass{...private:staticconstMyTypesome_constant;}case1.cppconstMyTypeMyClass::some_constant=...;案例2.h//Nomentionofsome_constantatall案例2.cppconstMyTypesome_constant=...;假设遵循常见的c++约定(1个header和cpp仅与1个类相关联,从不与#inclu

c++ - 检查类型是否可以作为 boost::lexical_cast<string> 的参数

我有以下特征类(IsLexCastable)来检查是否可以通过调用boost::lexical_cast将类型转换为字符串.它错误地返回true对于vector.#include#include#include#include#include#includeusingnamespacestd;usingnamespaceboost;namespacestd{///AddingtostdsincethesearegoingtobepartofitinC++14.templateusingenable_if_t=typenamestd::enable_if::type;}templates

c++ - C++ 中的 'static' 关键字

我意识到它在超出范围后保留了值(但变得无法访问),但我有几个问题。当人们说它在范围之外不可访问时,这只是意味着您不能在其标识范围之外更改值(它会出错)?我在考虑这段代码:#include"iostream"voidstaticExample();intmain(){staticExample();return0;}voidstaticExample(){for(inti=1;i我心想,在循环的每次迭代中,我都将“数字”变量设置为1。正如我最初预期的那样,它打印了1、2、3..10。编译器是否识别出将它设置为1的行是一个声明并忽略它的“更改”? 最佳答案

c++ - static const string 成员变量是否总是在使用前初始化?

在C++中,如果我想定义一些non-localconststring,可以在不同的类、函数、文件中使用,我知道的方法是:使用定义指令,例如#defineSTR_VALUE"some_string_value"const类成员变量,例如classDemo{public:staticconststd::stringConstStrVal;};//thenincppstd::stringDemo::ConstStrVal="some_string_value";const类成员函数,例如classDemo{public:staticconststd::stringGetValue(){ret

c++ - 执行 identity boost::lexical_cast 有什么开销?

给定一个函数,例如:templatevoidfunction1(constT&t){function2(boost::lexical_cast(t));}如果传递给function1的类型已经是std::string,会产生什么样的开销?开销是否会根据我要lexical_cast-ing的类型而有所不同?做一个重载函数来绕过强制转换是多余的吗?例如:voidfunction1(conststd::string&t){function2(t);}templatevoidfunction1(constT&t){function1(boost::lexical_cast(t));}boost

c++ - 计时代码 "C2440: ' <function-style-cast >' : cannot convert from ' _CR' to 'std::chrono::milliseconds' 中的一个奇怪错误

我偶然发现了一个奇怪的错误C2440:'':cannotconvertfrom'_CR'to'std::chrono::milliseconds'基本上相当于HowardHinnant'sanotherquestion中的代码.这应该在VisualStudio2012RC上编译吗?这个问题的原因是什么?修复或解决方法如何?我的目标只是创建一个简单的计时器(没什么太严肃的),所以如果存在这种效果,将采取点-以及其他实现线索。问题代码如下。用法:timers::stopwatchw;w.start();std::cout并且头文件是(为简洁起见省略了实现)namespacetimers{c