草庐IT

c++ - 模板函数特化 : linker error

当模板参数类型相同时,我正在尝试专门化两个模板参数的函数。我通过以下方式进行:#include#includeusingnamespacestd;templateintfun(U&u,Tt);templateinlineintfun(int&u,floatt){coutinlineintfun(U&u,typenamestd::enable_if::value,T>::typet){cout此代码编译良好(GCC4.8.2),但当U和T相同时,链接器会为所有fun调用提供undefinedreference类型。为什么它不起作用?链接器输出:g++-std=c++11test.cpp/

c++ - 部分模板特化取决于混合类型的整数常量的可变参数包

假设需要部分特化一个模板类型,该模板类型需要一个可变类型列表,用于所有参数都是特化的情况,比如说,std::integral_constant。以下直接的方法被各种版本的clang和GCC接受,但被VS14(2015)拒绝并出现错误:errorC3522:'t':parameterpackcannotbeexpandedinthiscontexttemplatestructfoo;templatestructfoo...>{/*...*/};foo,std::true_type>bar;我似乎无法在C++标准草案(n4296)中找到任何明确允许或禁止此类模式匹配的内容,因此在我提交针对

c++ - tuple_size 的特化不一致

当为某个模板显式特化tuple_size和tuple_element时,我意识到§14.5.1/4,它是这样写的Inaredeclaration,partialspecialization,explicitspecializationorexplicitinstantiationofaclasstemplate,theclass-keyshallagreeinkindwiththeoriginalclasstemplatedeclaration(7.1.6.3).似乎违反了标准本身:我是否遗漏了什么(例如,标准规定class-key“仅供说明”并且应在实际实现中达成一致)?

c++ - 通过 std::rel_ops 特化 std::greater

我如何专攻std::greater通过使用std::rel_ops?我有这样的东西#include#includeusingnamespacestd::rel_ops;structMyStruct{intfield;booloperator所以我需要按降序对元素进行排序。我如何使用operator来做到这一点,std::rel_ops和std::greater? 最佳答案 我假设你试图做类似的事情MyStructms[]={{10},{50},{30},{20}};std::sort(std::begin(ms),std::end(

C++ 多参数模板化类成员特化

我正在设置一个c++(11)程序,我在其中使用了一个依赖于2个参数的模板化类。该类的大部分内容都可以根据模板参数进行通用编写。只有少数功能需要专门的版本。这是重现我的问题的示例模式:templateclassfoo{//typedefsandmembersthatdependonTandN//butthatcanbewrittengenericallye.g.:typedefstd::arraymyarray;voidmyfunc(myarraytab);};//...templatefoo::myfunc(myarraytab){//genericversion}//needspec

c++ - 部分特化消歧优先链的更好模式?

考虑以下系列的偏特化:templatestructfoo{voidoperator()()const{coutstructfoo::value>>{voidoperator()()const{coutstructfoo::value>>{voidoperator()()const{coutstructfoo::valueandnot(sizeof(T)==4)andnotis_integral::value>>{voidoperator()()const{coutLiveDemo我经常看到这种情况(事实上,anotherStackOverflowanswerelsewhere为类似问题

c++ - 内部类的模板特化

考虑以下代码:structX{templateclassY{};};templateclassX::Y{};这里我们专门针对double类型的Y类,代码运行良好。问题是,如果我将代码更改为:templatestructX{templateclassY{};};templateclassX::Y{};编译器会报错:'X::Y':explicitspecializationisusingpartialspecializationsyntax,usetemplateinstead!有人知道在这种情况下我如何专攻Y类吗? 最佳答案 如果不显

c++ - 显式特化中不允许存储类

我在不属于类的头文件中有以下代码:templatestaticconstCompl*foobar(constFBTYPE&x);templatestaticconstCompl*foobar(constFBTYPE&x){returnx.funcA();}templatestaticconstCompl*foobar(constFBTYPE&x){returnx.funcB();}代码在较旧的GCC版本中编译得很好,但在较新的版本中我收到此错误消息:rsvt.h(672):error#3503:astorageclassisnotallowedinanexplicitspecializ

按非类型参数类型的 C++ 模板特化

相同模板函数的变体因非类型成员的类型不同是否有效?templatevoidf(unsignedint&v){v=V;}templatevoidf(bool&b){b=B;}目的是能够调用unsignedintmeaningOfLife;f(meaningOfLife);boolareYouAlive;f(areYouAlive);clang和gcc是沉默的,但MSVC报告warningC4305:'specialization':truncationfrom'int'to'bool'我想避免要求指定常量类型:f并希望确保常量值和目标值匹配。----麦克韦----#includetemp

c++ - 类模板特化从不同的基类继承是否合法?

我遇到过这样一种情况,我的类模板部分特化共享大量代码,将它们移到基类中是有意义的。然而,所有的特化都具有相同基类是没有意义的。以下示例代码在GCC7.1中编译无误:structfoo_base_1{voidbar(){std::coutstructfoo{};templatestructfoo:foo_base_1{};templatestructfoo:foo_base_2{};intmain(){foox;fooy;x.bar();y.bar();}我意识到尽管它们是同一类的特化,但它们实际上是不同的类型。仍然,感觉同一个类可以从不同的基础继承是错误的。我想要的是一些保证,这没关系