草庐IT

template-specialization

全部标签

c++ - 为什么类内偏特化是良构的?

根据[temp.class.spec]5/(强调我的)Aclasstemplatepartialspecializationmaybedeclaredorredeclaredinanynamespacescopeinwhichthecorrespondingprimarytemplatemaybedefined这表明部分特化(就像显式特化一样)必须出现在命名空间范围内。该段下方的示例实际上证实了这一点:templatestructA{structC{templatestructB{};};};//partialspecializationofA::C::Btemplatetemplat

c++ - 在模板参数列表中使用 sizeof... 时为 "too few template arguments"(MSVC 2017)

以下最小示例不基于当前的MSVC2017(19.16)。它确实基于MSVC2015和2017(19.14)的旧版本、GCC、Clang和ICC。所以我怀疑这是一个编译器错误。有效吗?如果不是,为什么?#include#includetemplateautofoo(std::integer_sequence){returnstd::array{Is...};}std::arraybar(){returnfoo(std::make_integer_sequence());}有效的变体:投入unsigned(sizeof...(Is))在参数列表中作为默认参数替换unsigned(...)与

c++ - 为什么 `uint64_t` 的模板特化与 Mac 平台上的 `unsigned long` 不匹配?

这个问题在这里已经有了答案:longlongintvs.longintvs.int64_tinC++(3个答案)关闭3年前。为什么以下代码片段在Mac平台上使用clang++编译失败?sizeofunsignedlong和uint64_t都是8,所以我认为它们是同一类型。那么为什么编译认为Serializer是抽象的吗?因为我定义了Serializer,Serializer,Serializer,Serializer,Serializer,Serializer,Serializer,Serializer,有没有办法解决这个问题并避免定义更多类型,如Serializer?错误信息如下,c

c++ - sizeof... 是否允许在模板参数中用于特化?

我正在尝试使用GCC4.7快照做一些类似的事情:templatestructfoo{staticconstintvalue=0;};//partialspecializationwherenisnumberofintsinxs:templatestructfoo{//error:templateargument‘sizeof(xs...)’//involvestemplateparameter(s)staticconstintvalue=1;};templatestructfoo{//Thiscompilesfine.sizeof(xs)issizeofint//eventhoughp

C++ 微软 : How to associate uuid/guid with template specialization

我想将uuid/guid与模板特化相关联。以下代码可用于将uuid与非模板接口(interface)(类、结构)相关联:__interface__declspec(uuid("CECA446F-2BE6-4AAC-A117-E395F27DF1F8"))ITest{virtualvoidTest()=0;};GUIDguid=__uuidof(ITest);//OK现在我有了一个模板化的界面template__interfaceITemplateTest{virtualvoidTest(Tt)=0;};我想做以下工作:GUIDtemplateGuid=__uuidof(ITemplat

c++ - 如何在 C++ 中避免 "template template template"模板

我已经尝试实现一个“模板模板模板”——模板类来满足我的需求(我对使用模板元编程很陌生)。不幸的是,我发现以下主题为时已晚:TemplateTemplateParameters不过,我需要实现如下所列的内容。根据编译器,最后一个typedef不工作。我不确定,但我认为这是由于3x模板限制的限制。在这个简单的示例中是否有可能绕过3xtemplate定义?templateclassITTranslator{public:ITTranslator()=0;virtual~ITTranslator()=0;virtualvoiddoSomething()=0;}templateclassTCon

c++ - 为什么我收到错误 "non-template ' f' used as template”

我正在尝试了解在哪里使用template和typename我遇到了一个我无法完全解决的问题。我有一个模板函数f它使用传递给它的类型(将是一个类)来调用模板成员函数.f.我想我使用typename在函数体中是正确的,但是,我不断收到以下错误:source.cpp:Infunction'voidf()':source.cpp:11:19:error:non-template'f'usedastemplatesource.cpp:11:19:note:use'typenameT::C::templatef'toindicatethatitisatemplatestructA{structC{

c++ - 此代码示例中使用了哪种模板特化?

我已阅读有关ExplicitSpecializationofClassTemplates的文档和PartialSpecializationofClassTemplates,但不明白这个例子中使用了什么样的特化(msdn链接仅由于我当前的环境而使用,问题或多或少是理论性的)。我需要c++标准中使用的名称和/或文档链接或对c++标准段落的引用。我试图解决的问题非常复杂,无法直接询问,但我知道如何使用与此示例中使用的方法类似的方法。templatestructis_vector{staticboolconstvalue=false;};templatestructis_vector>{sta

c++ - 为什么 static const char * template struct 成员没有初始化

我有一些C++11模板代码,我正在尝试移植到VisualC++Compiler2015。原始代码工作得很好,但是我需要重写它以解决constexpr的问题。Theoriginalcode(simplifiedexample)#includestructString{staticconstexprconstchar*value{"STRING"};};templateclassDerived{public:staticconstexprconstchar*value{Base::value};};templatestructFoo{staticconstexprconstchar*val

c++ - 具有函数和模拟部分模板特化的 SFINAE

我正在尝试创建一个函数X,它在提供成员函数Y时专门化,如果未提供成员函数Y,则函数X使用全局非成员函数Y来实现相同的效果。我目前正在尝试使用以下代码实现此目的template().y()),decltype(std::declval().y())>::value>*=nullptr>voiddo_something(Container&&container){returnstd::forward().y();}template().y()),decltype(std::declval().y())>::value>*=nullptr>voiddo_something(Container