草庐IT

static_pointer_cast

全部标签

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++ - GCC 7,aligned_storage 和 "dereferencing type-punned pointer will break strict-aliasing rules"

我编写的代码在GCC4.9、GCC5和GCC6中没有警告。它在一些较旧的GCC7实验快照(例如7-20170409)中也没有警告。但在最近的快照(包括第一个RC)中,它开始产生关于别名的警告。代码基本上可以归结为:#includestd::aligned_storage::typestorage;intmain(){*reinterpret_cast(&storage)=42;}使用最新的GCC7RC编译:$g++-Wall-O2-cmain.cppmain.cpp:Infunction'intmain()':main.cpp:7:34:warning:dereferencingtyp

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++ - 模板化的 Pointer 类可以有一个虚拟析构函数吗?

在使用自制指针类实现pimpl惯用语时,我遇到了一个令人惊讶的启示(我知道:为什么要自己动手?但请耐心等待)。以下三个文件包含一个最小示例:指针.h:#pragmaoncetemplateclassPointer{public:Pointer(T*p=0):_p(p){}virtual~Pointer(){delete_p;}private:voidoperator=(constPointer&);Pointer(constPointer&);private:T*_p;};Foo.h:#pragmaonce#include"Pointer.h"structFoo{Foo();~Foo(

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++ - Clang 提示 : "pointer is initialized by a temporary array"

我有一个不同长度的(指向)数组的数组,我了解到我可以使用复合文字来定义它:constuint8_t*constminutes[]={(constuint8_t[]){END},(constuint8_t[]){1,2,3,4,5END},(constuint8_t[]){8,9,END},(constuint8_t[]){10,11,12,END},...};gcc很好地接受了这一点,但clang说:指针由一个临时数组初始化,它将在完整表达式结束时被销毁。这是什么意思?代码似乎可以正常工作,但话又说回来,许多事情似乎在指向不再分配的内存时可以正常工作。这是我需要担心的事情吗?(最终我真

c++ - std::vector of function pointers:不同的模板参数

为什么下面会编译std::vectorfunc_ptrs;但这不是std::vectorfunc_ptrs?在第二种情况下,我收到了那些丑陋的STL错误消息之一,所以我不打算将所有内容都放在这里,但在消息的末尾我得到了这个/usr/include/c++/4.8/bits/stl_construct.h:102:30:error:ISOC++forbidsincrementingapointeroftype‘int(*)(double)’[-fpermissive]for(;__first!=__last;++__first)这似乎暗示C++将类型int(double)转换为int(*