草庐IT

const_iterator

全部标签

c++ - 模板类之外的内部类方法定义

我有课Array定义内部类const_iteratortemplateclassArray{//myclassherepublic:classconst_iterator{//myclasshere};voidinsert(const_iteratorposition,intvalue);};templatevoidArray::insert(const_iteratorposition,intvalue){//impl}这是否正常,我在类之外定义了函数并使用了const_iteratorposition作为第一个参数类型而不是写typenameArray::const_iterato

c++ - const T*& 和 const T* 不会在函数重载时产生歧义

为什么下面的示例代码不会产生歧义有没有办法调用第二个版本?(如果这不是错误)#includeusingnamespacestd;voidfoo(constint*){cout编辑:这个#includeusingnamespacestd;voidfoo(constint){cout确实会产生歧义。顺便说一下,去掉const产生的歧义。编译器:带有标志--std=c++14的g++5.3.0 最佳答案 Whythefollowingsamplecodedonotproduceambiguity这不是错误。参数的类型是constint*&

c++ - 对指针的 const 引用可以改变对象

const引用确保您无法更改所引用的对象。例如:inti=1;constint&ref=i;ref=42;//error,becauseofaconstreference但是如果你使用对指针或unique_ptr的引用,你可以。示例:classTinyClass{public:intvar=1;voidf1(){var=42;}};std::unique_ptrpointer(newTinyClass);conststd::unique_ptr&constRef=pointer;constRef->f1();//noerror我假设发生这种情况是因为指针本身没有改变。但是这个感觉mis

c++ - 了解 void f(const T& param) 中参数的类型

引用:EffectiveModernC++Item4.https://github.com/BartVandewoestyne/Effective-Modern-Cpp/blob/master/Item04_Know_how_to_view_deduced_types/runtime_output02.cppclassWidget{};template//templatefunctiontovoidf(constT¶m)//becalled{}std::vectorcreateVec()//factoryfunction{std::vectorvw;Widgetw;vw.pus

c++ - std::result_of 应用于 const 重载方法

如果我给typedefstd::vectorv;然后下面可以用来捕获常量迭代器的类型(另一种方法是使用v::const_iterator,但这取决于const_iterator成员类型在类中明确定义。typedeftypenamestd::result_of::typeconst_iterator;确实,我们可以检查上面的内容是否如我们所愿。static_assert(std::is_same::value);但是,我发现下面的编译器失败。typedeftypenamestd::result_of::typeiterator;编译器提示该方法被重载(通过const修饰符)并且无法明确解

c++ - 为什么这个用于检测类型 T 是否具有 void operator(EDT const&) 的 C++ 特性会失败?

我正在尝试使用SFINAE来检测作为模板参数T传递的类型是否具有T::operator()(Pconst&),其中P也是模板参数。我在MemberDetectorIdiom的这个例子之后为我的解决方案建模不幸的是,我无法让它为operator()工作,即使我可以让它为普通方法工作。下面是一些演示我面临的问题的示例代码:#include#include#include#includeusingnamespacestd;structhas{voidoperator()(intconst&);};structhasNot1{voidoperator()(int);};structhasNot

C++ 在 ‘value_type’ 中没有名为 ‘struct std::iterator_traits<int>' 的类型

你好。我正在尝试运行以下代码(仅用于培训目的):#include#includetemplate>classkont>typenamestd::iterator_traits::value_typefoo_test(typenamekont::iteratorb){return*b;}templatetypenamestd::iterator_traits::value_typeminimum(Iterb,Itere){Iterm=b;/*CODE*/return*m;}intmain(void){std::listx;x.push_back(10);x.push_back(100);

c++ - 带有尾随返回类型的 final、override、const 的语法

我正在尝试覆盖虚拟,但也使用关键字override、final和const,以及尾随返回类型。问题似乎出在派生类中,编译器错误(说我没有指定尾随返回类型)并没有太大帮助。代码在这里:https://wandbox.org/permlink/zh3hD4Ukgrg6txyE也贴在下面。我玩过不同的顺序,但似乎仍然无法正确处理。任何帮助将不胜感激,谢谢。#includeusingstd::cout;usingstd::endl;usingstd::ostream;////////////////////////////////////////////////BasestuffclassBa

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::interprocess : cout a string variable when iterating through a map that references an object from a struct

我正在使用boost::interprocess在进程之间共享对象。我有两个文件,一个生成结构对象并将该对象传递到具有int索引的映射中的“server.cpp”;和一个“client.cpp”文件,它检索内存数据并遍历数据,输出到控制台。结构看起来像这样:structmydatao{stringMY_STRING;intMY_INT;};和对象:mydatao;o.MY_STRING="hello";o.MY_INT=45;服务器和客户端都能正确编译。但是出于某种原因,如果我尝试访问客户端中的字符串而不是float或整数,客户端可执行文件会抛出段错误。例如下面的second.MY_I