草庐IT

template-auto

全部标签

c++ - 为什么不能形成对 'decltype(auto)' 的引用

intmain(){decltype(auto)&&a=100;}以上代码,在GCC和Clang中出错。intmain(){decltype(int)&&a=100;}此代码正确。在N4296中,在§8.3.2/6Ifatypedef(7.1.3),atypetemplate-parameter(14.3.1),oradecltype-specifier(7.1.6.2)denotesatypeTRthatisareferencetoatypeT,anattempttocreatethetype“lvaluereferencetocvTR”createsthetype“lvaluere

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

python - Cython 扩展类 : How do I expose methods in the auto-generated C struct?

我现有的C++代码定义了一些我需要使用的类,但我需要能够将这些类发送到Python代码。具体来说,我需要在C++中创建类实例,创建Python对象作为这些C++对象的包装器,然后将这些Python对象传递给Python代码进行处理。这只是一个更大的C++程序的一部分,因此最终需要使用C/PythonAPI在C++中完成。为了让我的生活更轻松,我使用Cython定义扩展类(cdef类)作为我的C++对象的Python包装器。我使用的是典型格式,其中cdef类包含指向C++类的指针,然后在创建cdef类实例时对其进行初始化。因为如果我有一个现有的C++对象要包装,我也希望能够替换指针,所以

c++ - Xcode C++ vector : Implicit instantiation of undefined template

我在不同的IDE上运行这段代码,它成功了。出于某种原因,我在Xcode上收到上述错误消息。我假设我缺少某种标题,但我不确定是哪一个。#include#include#include#includeintmain(){vectorlistRestaurants;//error:Implicitinstantiationofundefinedtemplatereturn0;} 最佳答案 Xcode10.2.1向我展示了错误Implicitinstantiationofundefinedtemplate'std::__1::vector,

c++ - auto const & map 迭代器的类型是什么? C++

我需要修复我的旧项目中的一些错误,我认为这是重构部分代码的最佳时机。我有一个具有以下结构的map:std::map>ComponentMap;在我需要遍历一些底层子map的某个地方,我使用了以下内容:for(std::map::iteratoriter=ComponentMap[compNameString].begin();iter!=ComponentMap[compNameString].end();++iter){//somecodeif(IsComponentOfType(iter,sCOMP_PRINCIPAL))iter->second->GetComponentValu

c++ - gcc 与 clang : noexcept parsed in unused template specialization when static casting

我正在尝试将函数指针静态转换为特定函数重载,但似乎clang仍会解析(未使用的)模板特化的noexcept语句,从而生成编译器错误。如果未使用相应的函数重载,GCC似乎并不关心noexcept。templatevoidfun(T)noexcept(T(1)){}voidfun(int){}voidfun(int*){}intmain(){inta;fun(&a);//callingworksfinefun(a);static_cast(&fun);//staticcastingdoesn't}https://godbolt.org/z/ixpl3f这里是哪个编译器出错了?当将函数指针转

c++ - "Name The Template Parameter"奇定义

template//whyisboolhere,classbooltype=bool.Aretheyequivalent?structif_{typedeftypenamettype;};templatestructif_//whatdoesthemean?{typedeftypenameutype;};代码来自一篇名为“命名模板参数”的文章。我无法理解both结构的定义。 最佳答案 编辑:首先移动最重要的部分:if_::type;//thisisintif_::type;//thisisdouble这很方便地定义了一个可以在编译时

c++ - 专门化成员 S::display 需要 ‘template<>’ 语法

我正在创建一个特征类来帮助我的程序。我有一个名为operations的模板类包含方法display和area.当我定义这些函数时,我得到了错误。他们在这里:error:specializingmember‘traits::operations::display’requires‘template’syntaxerror:specializingmember‘traits::operations::area’requires‘template’syntax如您所见,编译器要我插入template就在这些定义之前。但是当我这样做时,我会收到一大页错误。出了什么问题,我该如何解决?这是我的程

c++ - 在类型说明符中使用 "simple-template-id"

在C++11标准中,dcl.type.simple和dcl.type.elab部分声明类型说明符可以包括simple-template-编号。另一方面,根据temp.names部分,simple-template-id可以表示函数模板特化。真的可以使用函数模板特化来指定类型吗? 最佳答案 7.1.6.2/2Theothersimple-type-specifiersspecifyeitherapreviously-declareduser-definedtypeoroneofthefundamentaltypes(3.9.1).强调

c++ - 为什么bitset要用template来实现?

要定义一个16位的位集,就像这样:std::bitsetbs(0x123);如果让我设计一个bitset类,我大概会这样:mine::bitsetbs(16,0x123);std::bitset是由模板实现的有什么原因吗?这是我们应该在某些情况下应用的好模式吗? 最佳答案 Isthereanyreasonthatstd::bitsetisimplementedbytemplate?Isthisagoodpatternthatweshouldapplyinsomesituations?因为std::bitset被设计成一个静态位集。在