草庐IT

weak-template-vtables

全部标签

c++ - 错误 : Use of class template requires template argument list

当我尝试运行我的程序时,此错误显示为“errorC2955:'FOURTEEN':useofclasstemplaterequirestemplateargumentlist”#includeusingnamespacestd;templateclassFOURTEEN{private:Ta[n];public:voidReadData();voidDisplayData();};voidFOURTEEN::ReadData(){for(inti=0;i>a.[i];}voidFOURTEEN::DisplayData(){for(inti=0;i>a.[i]P;//Readdatai

c++ - 我可以将 `extern template` 放入头文件中吗?

在头文件中放一个外部模板,然后在单元编译文件中显式模板实例化有效吗?例如在g++的编译示例中,这是为了避免nothing的实例化吗?两次?为什么没有人这样写而更喜欢复制externtemplate每个.cpp文件中的行?A.hpp:#ifndefHEADERC_A#defineHEADERC_Atemplatestructnothing{};externtemplatestructnothing;#endifA.cpp:#include"A.hpp"templatestructnothing;ma​​in.cpp:#include"A.hpp"#includeintmain(){not

c++ - 如何与 "template using"定义的模板(别名)类成为 friend ?

类B想和每个人成为friendC.我正在努力寻找解决方法。只要我不添加有问题的行,下面是成功编译的完整代码。#includeusingnamespacestd;enumEN{EN1,EN2};templateclassC{public:C(){std::coutclassB{templateusingCT=C;//templatefriendclassCT;//ct;}};intmain(){B::test();return0;}这是我尝试过的(全部失败):-templatefriendclassC;templatefriendclassCT;templatefriendclassCT

c++ - C++ 模板的部分特化 : template parameter not deducible

下面的代码工作正常:templateclassFib{};templateclassFib{};但是下面的代码显示错误为:Error:templateparametersnotdeducibleinpartialspecialization:templateclassFib{};templateclassFib{};你能解释一下这种行为的原因吗? 最佳答案 我相信您只是缺少部分特化的正确语法:templateclassFib{};templateclassFib{};模板上的第一个参数是类型,而第二个只是一个常量值。

c++ - template<class = enable_if_t<...>> 做什么?

我一直在阅读STL文件,以学习格式化代码的更好方法,并学习提高效率的技巧。我一直在阅读线程文件,但我无法弄清楚某些代码的作用。template,thread>::value>>explicitthread(_Fn&&_Fx,_Args&&..._Ax){//constructwith_Fx(_Ax...)...}std::enable_if_t是templateusingenable_if_t=typenameenable_if::type;templatestructenable_if{//typeis_Tyfor_Testusingtype=_Ty;};该代码在thread和str

c++ - 为什么这个悬空的 std::weak_ptr 不会导致 SEGFAULT?

这个问题在这里已经有了答案:Whendoesinvokingamemberfunctiononanullinstanceresultinundefinedbehavior?(2个答案)关闭4年前。在下面的代码中,我在范围内创建了一个shared_ptr并将其分配给一个weak_ptr。为什么在运行代码时我没有得到SEGFAULT,因为wp应该在范围之外无效,对吧?namespace{structDummy{intx;voidfoo(){std::coutwp;{autosp=std::make_shared();wp=sp;}wp.lock()->foo();};

c++ - 在基类中调用 shared_from_this() 时的 bad_weak_ptr

我有一个SuperParent类,一个Parent类(派生自SuperParent)并且都包含一个shared_ptr到一个Child类(它包含一个weak_ptr到一个SuperParent)。不幸的是,我在尝试设置Child的指针时遇到了bad_weak_ptr异常。代码如下:#include#include#include#includeusingnamespaceboost;classSuperParent;classChild{public:voidSetParent(shared_ptrparent){parent_=parent;}private:weak_ptrpare

c++ - 将 weak_ptr 与原始指针进行比较不起作用,正在寻找替代方案

我有一个SpriteManager类,它为我加载和缓存Sprite,并从缓存中删除未使用的Sprite。无论如何,这就是我的想法,我有点卡住了。我有一个map>我在其中存储Sprite,并使用weak_ptr生成shared_ptr的。现在我正在尝试使用一个删除器,它也从map中删除位图,它看起来像这样(显然不起作用):[&bitmaps](ALLEGRO_BITMAP*bmp){for(autoit=bitmaps.begin();it!=bitmaps.end();++it){if((*it).second==bmp){bitmaps.erase(it);al_destroy_bi

c++ - 实现Matlab的冒号: operator in C++ expression templates class

我正在实现一个C++表达式模板库。我已经设置了一个适当的SubMatrixExpr类来收集矩阵中的元素,启用类似的语法B=SubMatrix(A,1,3,2,10);相当于Matlab的B=A(1:3,2:10);当然,Matlab的语法比我的要舒服得多。所以我的问题是有没有可能在C++中设置Matlab的冒号:运算符?非常感谢您。 最佳答案 简短的回答:没有。冒号不是有效的C++运算符,因此不能重载。即使可以,它仍然不可能轻松实现您的需求,因为它肯定会优先于逗号运算符,这将使您的表达式位于A((1:3),(2:10))行中。,如果

c++ - Cryptic template 模板参数错误

我正在尝试创建一个从std::map或std::unordered_map获取键的函数。我可以使用简单的重载,但首先我想知道这段代码有什么问题。templateclassTContainer>std::vectorgetKeys(constTContainer&mMap){std::vectorresult;for(constauto&itr(std::begin(mMap));itr!=std::end(mMap);++itr)result.push_back(itr->first);returnresult;}当使用std::unordered_map调用它时,甚至手动指定所有模板