草庐IT

is_const

全部标签

c++ - const char * 与其他指针不同吗?

这个问题在这里已经有了答案:Whydoescoutprintchararraysdifferentlyfromotherarrays?(4个答案)关闭4年前。所以我最近一直在深入研究指针和引用,因为它们经常出现在我用来学习OpenGL的资源中。我注意到constchar*指针的行为似乎与其他指针有些不同。为了演示,我创建了这个测试程序:#includeintmain(){inti=2;int*pi;//constcharc="helloworld";constchar*pc="helloworld";pi=&i;std::cout输出:'helloworld'typeischarcon

c++ - boost static_vector 而不是 std::is_trivially_destructible

根据thisexample(左例)#include#includestructX{intk;std::arraya;boost::container::static_vectorb;~X()=default;};inthuh(){std::arrayx;return0;}看起来像boost::container::static_vector当T时可以轻易破坏是(当b被销毁时,不会在X上循环)。huh优化为xoreax,eax;ret(即return0不遍历数组。当我改用具有非平凡析构函数的包含类型时(右例)#include#includestructY{~Y();};structX{i

c++ - 编译器错误 : ‘std::array<...>::~array()’ is implicitly deleted

我有以下.hpp文件:#ifndefCODE_HPP#defineCODE_HPP#include#includeusingstd::vector;usingstd::array;template>classstack;template,typenameK=stack>classstack_array;templateclassstack{Cpile;stack();~stack();voidpush(T&);friendclassstack_array>;};templateclassstack_array{private:staticconstsize_tmax_elem=10;a

c++ - 警告: "when type is in parentheses, array cannot have dynamic size"?的原因是什么

我已经发布了一个关于与数组的动态内存分配相关的GCC错误的问题:Anerrorisissuedbygccrelativetoparsingtype-idinanewexpression现在使用ClangHEAD10.0.0我收到以下警告:rog.cc:9:37:warning:whentypeisinparentheses,arraycannothavedynamicsizeint(**a)[N3]=new(int(*[n1])[N3]);~~^~~当我运行这个演示程序时:#includeintmain(){constsize_tN3=4;size_tn1=2;int(**a)[N3]

c++ - BOOST_FOREACH : What is the error on using this of a STL container?

有谁知道为什么以下会在VC9上产生错误?classElem;classElemVec:publicvector{public:voidfoo();};voidElemVec::foo(){BOOST_FOREACH(Elem&elem,*this){//Dosomethingwithelem}return;}我得到的错误是:errorC2355:'this':canonlybereferencedinsidenon-staticmemberfunctions我现在拥有的唯一(hack)解决方案是:voidElemVec::foo(){ElemVec*This=this;BOOST_FO

c++ - 如何从成员函数模板类型签名中删除 const?

我正在研究一些C++类型系统的东西,但我在从成员函数中删除const-ness以用于函数特征类时遇到问题。真正麻烦的是,这在G++中运行良好,但MSVC10无法正确处理部分特化,我不知道这些编译器中的一个是否真的存在错误。这里的问题是,以我可以获得函数类型签名的方式从成员函数中删除const限定符的正确方法是什么?采用以下代码示例:#includetemplatestructRemovePointer{typedefTType;};templatestructRemovePointer{typedefTType;};templatestructRemovePointer{typedef

c++ - 将临时对象附加到对 const 的引用时出错

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:typedefandcontainersofconstpointers为什么代码会发出错误?intmain(){//testcodetypedefint&Ref_to_int;constRef_to_intref=10;}错误是:error:invalidinitializationofnon-constreferenceoftype‘int&’fromatemporaryoftype‘int’我阅读了prolongingthelifetimeoftemporaries上的帖子这表示临时对象可以绑定(bind

1071 - Specified key was too long; max key length is 3072 bytes Mysql报错解决方法

错误信息“Specifiedkeywastoolong;maxkeylengthis3072bytes”是在MySQL数据库中创建索引时可能出现的问题,通常出现在尝试创建一个过长的唯一键(UNIQUEKEY)或主键(PRIMARYKEY)时。MySQL对于InnoDB存储引擎有一个索引键长度的限制,这个限制基于字符集的不同而不同。例如,在使用utf8字符集时,每个字符可能占用3个字节,那么对于innodb表,索引键的最大长度大约为1000个字符左右(因为3072/3≈1024)。若字符集是utf8mb4,每个字符可能占用4个字节,所以最大长度会进一步减少到768个字符左右(3072/4=768

c++ - 是否可以编写这些 pure_assert 和 const_assert 宏?

GCC__attribute__((pure))和__attribute__((const))分别允许将函数声明为无副作用和引用透明;假设我想编写pure_assert和const_assert宏,其参数必须是适当严格级别的表达式,即:assert(oops_a_side_effect());静默导致调试和发布中的不同行为,但是:pure_assert(oops_a_side_effect());const_assert(oops_read_a_global());至少在调试版本中会出现编译时错误。由于我希望是显而易见的原因,您不能只创建一个声明为__attribute__((pure

c++ - const 引用右值的类数据成员的生命周期是多少?

通常这个讨论只针对局部函数变量:voidfoo(constint&i){//useitillfoo()ends}foo(3);但是,这条规则是否也适用于class成员?structA{constint&a;A():a(3){}//version1A(constint&i):a(i){}//version2};现在A用作,{return()?newA:newA(3):newA(some_local_variable);}a的内容是否会在all3的整个生命周期内保持不变新分配A? 最佳答案 C++03标准(“12.2/5临时对象”部分)