草庐IT

Const-Qualification

全部标签

c++ - 调用 const 可变 lambda

为了简化测试用例,假设我有以下包装类:templatestructWrapper{decltype(auto)operator()()const{returnm_t();}decltype(auto)operator()(){returnm_t();}Tm_t;};templateautomake_wrapper(Tt){returnWrapper{t};}假设我包装了以下简单的仿函数返回引用:structFoo{int&operator()(){returnx;}constint&operator()()const{returnx;}intx;};在我的main函数中,我试图将Foo

c++ - 为什么T 'skips'的模板参数推导是数组元素的const,而函数参数是对T的const引用?

让我们考虑一下这些定义:/***fulltypeinformationwithtypeid***/templateclassType{};templatestd::stringtypeStr(){returntypeid(Type).name();}/***functiontemplateforparameterdeduction***/templatevoidfunc(constT&a){std::cout()()指向常量的指针如果执行以下语句:constinti=5,*ip=&i;func(ip);输出是:DeducedtypeforTis:4TypeI**PKi**E所以T实际上

c++ - 构造函数何时调用 const-expr?

什么时候构造函数调用const-expr什么时候不是?在这个链接问题中Arethesevectordefinitions"constantinitialization"?构造函数不是const-expr,这就是vector未const初始化的原因。还有人可以更详细地解释该问题的答案吗? 最佳答案 您是在问何时可以在需要常量表达式的上下文中使用构造的结果?例如classA{constexprA(...){...};constexprintget(){...};...}constexprAa(...);std::arrayx{};换句话

c# - 如何将 C++/CLI 字符串转换为 const char*

我有一个C++/CLIDLL,我打算将其用作我的C#DLL和nativeC++客户端之间的适配器。我需要在两个方向上传递字符串。该适配器是使用VS2013编译的,但需要支持使用VS2008构建的客户端,因此我在API中使用constchar*。但是即使两者都是VS2013构建的,我所得到的也无法正常工作。我在其他地方找到了使用msclr\marshal.h的建议,因此我创建了:usingnamespacemsclr::interop;System::String^ToCliString(constchar*s){System::String^result=marshal_as(s);r

c++ - 在 C++ 的元编程中保护从非 const-volatile 类型到 const-volatile 的赋值的正确方法是什么?

例如我有一个函数可以实现null_ortemplateautonull_or(T*p,U*default_value)->typenamestd::enable_if::type,typenamestd::decay::type>::value,T*>::type{if(p){returnp;}else{returndefault_value;}}仅使用std::decay将启用const/volatiletype*default_value分配给non-const/non-volatiletype*p.避免它的最佳方法是什么?此外,像typedefault_value[100]这样的

c++ - std::shared_ptr<std::string const> 能否作为引用计数不可变字符串的有效实现?

理想情况下,不可变字符串类只需要为每个字符串分配一个内存。甚至引用计数也可以存储在与字符串本身相同的内存块中。string的简单实现和shared_ptr将为shared_ptr分配三block不同的内存:字符串缓冲区的内存字符串对象的内存引用计数的内存现在,我知道在使用std::make_shared()时,智能实现可以将最后两个组合成一个分配。但这仍然会留下两个分配。当您知道字符串是不可变的时,字符串缓冲区将不会被重新分配,因此应该可以将它与字符串对象集成在一起,只留下一次分配。我知道一些字符串实现已经对短字符串使用了这样的优化,但我正在寻找一个不管字符串长度如何都这样做的实现。我

c++ - 将 `hana::string` 转换为 `constexpr const char (&)[]`

我有一些旧代码使用了与str_const非常相似的东西描述here和here做一些constexpr字符串操作。str_const是ScottSchurr描述的文字类型,可以从字符串文字构造,因为它具有来自constchar(&)[]的模板构造函数.我现在也有一些使用boost::hana的新代码.我希望能够参加hana::string并创建一个str_const那是指它。最简单的方法是转换hana::string到constexprconstchar(&)[].(实际上,在这一点上这不是最简单的方法,最简单的方法肯定是在我的str_const实现中添加一个新的模板构造函数。但在这一点

c++ - 在类声明中初始化 const 成员变量时 Debug模式下的异常

#include#include#include#includeclassX{public:X(){std::cout>ValidatorType;constValidatorTypem_validators=ValidatorType{{"some-string",[](){//validationcodestd::cout以上代码使用Xcode7.2.1和Clang7.0.2在OSX上以调试和Release模式成功构建和运行。它还使用VisualStudioExpress2013forWindowsDesktop在Windows7上以Release模式成功构建和运行。但是,在Win

c++ - const、span 和迭代器问题

我尝试编写一个迭代器,通过索引遍历容器。It和一个constIt两者都允许更改容器的内容。Const_it和一个constConst_it两者都禁止改变容器的内容。在那之后,我尝试写一个span在一个容器上。对于类型T这不是const,两者都是constspan和span允许更改容器的内容。两者constspan和span禁止改变容器的内容。代码无法编译,因为://*thisisconstwithinaconstmethod//ButItrequiresanon-const*thishere.//SothecodedoesnotcompileItbegin()const{returnI

c++ - 避免接受 lambda 的方法的 const/non-const 重复

classFrame表示像素类型为P的图像.由于底层数据缓冲区格式的多种灵active,遍历其像素的算法并非易事。template//Pispixeltype;RM=is_row_majorclassFrame{//...templatevoiditerate(Ff){//iterateinawaythatisperformantforthisbufferif(stride==(RM?size.w:size.h)){auton=size.area();for(index_tk=0;k(stride)*(RM?size.h:size.w);for(index_tk0=0;k0我希望能够同