草庐IT

c++ - 如何访问其他模板类实例的私有(private)成员?

这个问题在这里已经有了答案:HowtoshareprotectedmembersbetweenC++templateclasses?(1个回答)关闭8年前。这个极小的示例将无法编译,因为A无法访问私有(private)成员i在AtemplateclassA{inti;public:templatevoidcopy_i_from(constA&a){i=a.i;}};intmain(void){Aai;Aad;ai.copy_i_from(ad);return0;}我找不到使这些模板实例成为friend的正确方法。

c++ - 为什么 C++11 没有模板类型定义?

为什么C++11没有“模板类型定义”,比如templatetypedefstd::vector>vec;相反,他们只允许新语法:templateusingvec=std::vector>; 最佳答案 n1406是HerbSutter提出的“typedef模板”的提议,它模仿了您问题中的语法。n1499建议“模板别名”取代它,其中包含using当前存在于C++11中的语法。“typedef模板”的主要缺点之一在这两篇论文中都得到了解决。来自n1406:Inexistingpractice,includinginthestandardl

c++ - 命名空间中的函数模板特化

我想特化一个函数模板。此函数在命名空间中声明:namespacefoo{templatevoidfunction();}(为简单起见,模板基于int,而在我的生产代码中,它是一个enumclass,但这是同一个问题。相同适用于基于类型的模板)现在我想针对特定值专门化它:templatevoidfoo::function(){}使用g++-std=c++11(版本4.6、4.7、4.8和4.9)编译失败:specializationof‘templatevoidfoo::function()’indifferentnamespace[-fpermissive]clang++-std=c+

c++ - 专用内部类模板函数的类外定义?

请考虑以下格式错误的程序:structS{templatestructJ{};};templatestructS::J{voidf();};templatevoidS::J::f(){}//ERROR$clang++-std=c++11test.cppnofunctiontemplatematchesfunctiontemplatespecialization'f'$g++-std=c++11test.cpptemplate-id‘f’for‘voidS::J::f()’doesnotmatchanytemplatedeclaration为什么f的定义不能编译?如何在上面正确定义函数f

c++ - 如何从模板化类方法返回依赖类型?

假设我有一个基于模板ThingType的类。在header中,我使用它来typedef依赖类型VectorThingType。我想从GetVectorOfThings()方法中返回它。如果我将VectorThingType设置为返回类型,我会得到一个Doesnotnameatype错误,因为该类型未在此范围内定义。有什么方法可以在不复制typedef中的代码的情况下做到这一点?#include#includetemplateclassThing{public:ThingTypeaThing;typedefstd::vectorVectorThingType;VectorThingTyp

c++ - 如何检查两种类型是否来自同一个模板类

这个问题在这里已经有了答案:Checkiftwotypesareofthesametemplate(4个答案)关闭3年前。我想检查两种类型是否相同,但不管它们的模板参数如何。像这样:templateclassA{};classB{};intmain(){cout,A>::value,B>::value我知道std::is_same用于检查两种类型是否完全匹配。我需要这个的原因:我有一个可以用任何类型调用的模板化方法,但我想禁止使用A类型(它是模板化的)调用它,可能是通过使用static_assert。如果A没有模板化,我相信可以使用std::is_same轻松完成,但是现在,我遇到了一

c++ - 为什么这个可变参数模板参数的替换失败了? (在固定参数之前打包)

这是触发编译错误的最小示例:#includevoidfoo(int,double,int){}templatevoidpost_forwarder(void(*fun)(Args...,int),Args&&...aArgs){fun(std::forward(aArgs)...,5);}intmain(){post_forwarder(foo,6,6.1);//Compilationerroroninstantiationreturn0;}我怀疑问题与可变参数模板参数在固定int参数之前在函数类型中扩展的事实有关,但如果是这种情况,我找不到很好的理由。Clang3.6报错是:erro

c++ - 可变模板构造函数优先级

给定以下简单的structtemplatestructA{A(Ta){}templateA(Ta,Ts...more){}};intmain(){Aa(1);}A(Ta)将被调用而不是可变模板构造函数的保证是什么,为什么? 最佳答案 您要查找的标准部分是§14.8.2.4IfAwastransformedfromafunctionparameterpackandPisnotaparameterpack,typedeductionfails.Otherwise,usingtheresultingtypesPandA,thededuct

c++ - 在模板类中声明的 friend 模板函数导致 undefined symbol 链接错误

几天来我一直在努力反对这个问题,查找它并在开源项目中寻找类似的代码:无法真正找到我做错了什么。基本上,给定以下代码(提炼出其本质):#includeusingstd::cout;usingstd::endl;usingstd::string;templateclassNode{Tvalue_;public:Node(constT&value):value_(value){}Tconstvalue()const{returnvalue_;}friendstd::ostream&operator&node);Nodeoperator+(constNode&other){returnNode

c++ - 无法从 std::bind 推断出 std::function 的模板参数

我正在尝试找到一种方法来调用多个类成员函数,每个函数都有不同的参数,并且在调用前后会发生某些已知功能。这个包装函数是我试过的,但是例如对它的最终调用不会编译错误:'boolWrapper(Work*,std::function,Args&&...)':couldnotdeducetemplateargumentfor'std::function'from'std::_Bind,Work*const>'classWork{public:voidDoWork(inta,doubleb,stringc);private:voidPre(){};voidPost(){};boolStep1()