草庐IT

remove_const

全部标签

C++ const 转换,不确定这是否安全

这似乎是一个愚蠢的问题,但我真的需要澄清一下:这会给我的程序带来什么危险吗?甚至还需要const_cast吗?如果我就地更改输入指针值,它将安全地与std::string一起工作,还是会产生未定义的行为?到目前为止,唯一担心的是,每当我修改输入指针并使其无法使用时,这可能会影响字符串“some_text”。std::stringsome_text="Textwithsomeinput";char*input=const_cast(some_text.c_str());谢谢你给我一些提示,我想避免自取其辱 最佳答案 作为邪恶行为的一个

c++ - remove_if and then erase 在 vector 上是否有效?

虽然关于vector的remove_if+erase有几十个问题。我找不到这种Action的表现。当我写:myVector.erase(remove_if(myVector.begin(),myVector.end(),some_predicate),myVector.end());removeif将返回指向最后一个相关项+1(我们称它为X)的迭代器。我相信这会在O(n)内发生。但是删除将如何工作?如果删除将尝试从X删除到myVector.end()它将是O(n^2)因为它会导致将vector复制到新位置,并且将有O(n)次新分配从堆。但是如果它将从myVector.end()删除到X

c++ - MSVC 使用 constexpr 从可变参数模板方法中的基本模板参数中吞下 const if

我有一个问题,我几乎可以肯定这是一个MSVC错误,但也许我遗漏了什么。这是实际代码的简化版本:templateclassInnerType{};templateclassOuterType{public:voidfoo1(){ifconstexpr(true){bar(InnerType());}}voidfoo2(){if(true){bar(InnerType());}}voidbar(InnerType){}};如您所见,foo1()和foo2()之间的唯一区别是constexprif。以下是我尝试在MSVC中编译一些测试时发生的情况:OuterTypetest1;test1.f

c++ - 为什么 lambda 没有从到达范围捕获类型 const double,而 const int 是?

我似乎无法理解为什么以下类型为constint的代码可以编译:intmain(){usingT=int;constTx=1;autolam=[](Tp){returnx+p;};}$clang++-clambda1.cpp-std=c++11$而这个类型为constdouble的不是:intmain(){usingT=double;constTx=1.0;autolam=[](Tp){returnx+p;};}$clang++-clambda2.cpp-std=c++11lambda1.cpp:5:32:error:variable'x'cannotbeimplicitlycaptur

c++ - static_assert 无法将 const char* 模板参数识别为 constexpr : g++ bug?

考虑以下定义。charright_string[]="::right_one.";charwrong_string[]="::wrong_one.";templatevoidf(){static_assert(str==::right_string,"Passme::right_string!");}structTest{staticconstexprcharright_string[]="template_struct::right_one";staticconstexprcharwrong_string[]="template_struct::wrong_one";template

c++ - 使用移动赋值从算术运算符重载返回 const 值

假设我有以下最小示例类:#includeclassFoo{public:Foo()=default;Foo(constFoo&)=default;Foo(Foo&&)noexcept=default;Foo&operator=(constFoo&rhs){std::cout这会按预期打印move。现在根据EffectiveC++Item3,我应该从operator+返回constFoo以避免像a+b=c这样的构造,即://Toavoida+b=cconstFoooperator+(constFoo&rhs)const{}不幸的是,这突然开始调用复制赋值而不是移动赋值运算符。[我在Ubu

c++ - 如何以 clang 格式强制使用 east const?

是否有clang格式的标志将“constwest”更改为“eastconst”,以便以下内容:voidfun(conststd::string&s);将被重新格式化为:voidfun(std::stringconst&s); 最佳答案 从Clang-Format14开始,有QualifierAlignment。将以下行添加到您的.clang-format配置文件中:QualifierAlignment:Right 关于c++-如何以clang格式强制使用eastconst?,我们在Sta

c++ - 为什么 const temporary 选择调用非 const 成员函数而不是 const 成员函数?

这个问题在这里已经有了答案:Consttemporaryfromtemplatetypeandwhyusestd::add_const?(2个答案)关闭9年前。示例代码取自:http://en.cppreference.com/w/cpp/types/add_cv(我稍微修改了一下。)structfoo{voidm(){std::coutvoidcall_m(){T().m();}intmain(){call_m();call_m();//here}输出是:Non-cvNon-cv在第二次调用中,T是const限定的,所以T()应该调用const版本,对吗?还是我错过了一些特殊规则?

c++ - 强制转换为模板中的引用似乎抛弃了 const-ness

考虑以下C++代码:typedefstd::string&mutable_string_ref;conststd::stringstr="abc";mutable_string_refref(str);这显然会导致编译器错误,因为您无法创建对常量字符串的可变引用。对于GCC4.7.2,产生的错误是:error:invalidinitializationofreferenceoftype‘mutable_string_ref{akastd::basic_string&}’fromexpressionoftype‘conststring{akaconststd::basic_string}

c++ - 模板代码中的转发引用与 const 左值引用

我最近一直在研究C++中的转发引用,下面是我目前对该概念的理解的快速总结。假设我有一个模板函数foo对T类型的单个参数进行转发引用.templatevoidfoo(T&&arg);如果我用左值调用这个函数,那么T将被推断为T&制作arg参数类型为T&由于引用折叠规则T&&&->T&.如果使用未命名的临时函数调用此函数,例如函数调用的结果,则T将被推断为T制作arg参数类型为T&&.内部foo然而,arg是一个命名参数,所以我需要使用std::forward如果我想将参数传递给其他函数并仍然保持其值类别。templatevoidfoo(T&&arg){bar(std::forward(a