草庐IT

forwarding-reference

全部标签

c++ - 尝试与 typedef 交 friend 时出现 "elaborated type refers to typedef"错误

假设我有以下代码(一个简单的CRTP类层次结构)。我想对基类类型进行typedef以节省自己的输入(在我的实际代码中,我多次使用基类类型并且基类采用多个模板参数),并且我需要与基类交friend,因为我想保留实现私有(private)。templateclassBase{public:voidfoo(){*static_cast(this)->foo_i();}};templateclassDerived:publicBase>{public:typedefclassBase>BaseType;private://Thishereistheoffendinglinefriendclas

c++ - 如何有效地从 forward_list 中删除一个元素?

好吧,我认为这个问题已经很概括了。我有一个独特项目的forward_list,并想从中删除单个项目:std::forward_listmylist;//fillwithstuffmylist.remove_if([](Tconst&value){returnvalue==condition;});我的意思是,这种方法工作正常,但效率低下,因为一旦找到并删除项目,它就会继续搜索。有更好的方法还是我需要手动完成? 最佳答案 如果只想删除第一个匹配项,可以使用std::adjacent_find后跟成员erase_after#includ

c++ - 为什么以不同的顺序使用 std::remove_reference 和 std::remove_const 会产生不同的结果?

在下面的代码中,我使用了std::remove_const和std::remove_reference但在两种情况下以不同的顺序给出了不同的结果:#include#include#include#include#includeusingnamespacestd;intmain(){vectorar={"mnciitbhu"};cout::type>::typeTT;cout::value::value::value::type>::typeTT;cout::value::value::value输出是:Firstcase:truefalsefalseSecondcase:falsetr

c++ - std::decay 和 std::remove_reference 之间的区别

在用C++做模板元编程的时候,经常遇到类似下面的情况:templateSmake_wrapper(T&&t){returnS(std::forward(t));}我知道我应该在返回类型中使用类似std::decay的东西,但为什么std::remove_reference不能正常工作?这里有什么区别?std::remove_cvref怎么样? 最佳答案 举个例子#includeintmain(){static_assert(std::is_same_v,std::remove_reference_t>);//int!=constin

c++ - std::forward_iterator_tag 的作用是什么?

在分析一个应用程序时,我碰到了gcc4.7.1附带的那部分标准库实现。它是include/g++-v4/bits/vector.tcc:templatetemplatevoidvector::_M_range_insert(iterator__position,_ForwardIterator__first,_ForwardIterator__last,std::forward_iterator_tag){…}我注意到函数签名的最后一个参数只是一个标记,我开始想知道它为什么会在这里。快速浏览thispage表明std::forward_iterator_tag是一个空结构。它在这里的作

c++ - 视觉 C++ : forward an array as a pointer

我已经将一些无法在VisualStudio2015上编译的C++11代码缩减为以下我认为应该编译的代码(并且使用clang和gcc):#includevoidtest(constchar*x);intmain(){constcharx[]="Helloworld!";test(std::forward(x));}我知道这里不需要调用forward。这是从一段更复杂的代码中删除的,该代码将可变参数中的任何数组衰减为指针并转发所有内容。我确信可以通过模板特化或SFINAE找到解决此问题的方法,但我想在走那条路之前知道它是否有效的C++。编译器是VisualStudio2015,问题可以重现

c++ - 访问静态 constexpr float 成员时 undefined reference

此代码有效:structBlob{staticconstexprinta=10;};intmain(){Blobb;autoc=b.a;}但是如果我将int更改为float我会得到一个错误:structBlob{staticconstexprfloata=10.0f;};/tmp/main-272d80.o:Infunctionmain':main.cpp:(.text+0xe):undefinedreferencetoBlob::a'为什么我不能以这种方式使用constexprfloat?编译器:Ubuntuclang版本3.5.0-4ubuntu2(tags/RELEASE_350

c++ - 为什么 std::reference_wrapper 不是默认可构造的?

我觉得防止std::reference_wrapper默认构造使其更难使用,即使使用默认构造的reference_wrapper会导致运行时异常。然而,一个reference_wrapper是完全可复制的,因此它的值始终可以更改,那么为什么要阻止它默认具有the空引用?它使许多用例变得更加简单,并且有了它,建议的observer_ptr不再需要-为什么需要冗余?默认构造reference_wrapper会统治他们!想法? 最佳答案 However,areference_wrapperisperfectlycopyable,soit'

c++ - 对 `forkpty' 的 undefined reference

所以我在Ubuntu10.04的Eclipse中开发我的项目。我有以下代码行:#includepid_tpid;intmaster;pid=forkpty(&master,NULL,NULL,NULL);但是当我尝试在Eclipse中构建它时,出现错误:undefinedreferenceto'forkpty'知道如何解决这个问题吗? 最佳答案 您需要-lutil命令行参数(以使用libutil共享库)。对于eclipse:http://zetcode.com/articles/eclipsecdevelopment/选择项目属性。

c++ - 为什么我不能对 reference_wrapper<std::chrono::milliseconds> 的 vector 进行排序?

我想要一个std::vector的排序View但我不想修改原始容器。std::reference_wrapper看起来很适合这个,它对整数vector也适用。我创建了这个小例子:#include#include#include#include#includeintmain(){std::vectornumbers{1,42,3,9,5};std::vector>sorted_numbers(numbers.begin(),numbers.end());std::sort(sorted_numbers.begin(),sorted_numbers.end());std::coutdura