草庐IT

template-auto

全部标签

c++ - template<class key, class type> 在 C++ 中的方法之前是什么意思?

我有这段代码,我试图理解遵循的约定,.cpp文件中定义的所有方法都有template写在他们面前。这是什么意思?例子://ConstructortemplateMyOperation::MyOperation(){//methodimplementation}//AmethodtemplateMyOperation::otherOperation(){//methodimplementation}谢谢 最佳答案 必须已经有一个很好的答案,但我也会把我的也扔进池中。C++允许程序结构的声明和实现分开进行。它源于C/C++程序员如何相互

C++ Templates 多态障碍

接口(interface):templateclassInterface{public:typedefTUnits;virtualTget()=0;};实现1:classImplementation1:publicInterface{public:floatget(){return0.0f;}};实现2:classImplementation2:publicInterface{public:intget(){return0;}};容器(有错误):classContainer{private:Interface*floatGetter;intn;Timer::Units*array;pu

c++ - 遍历动态 vector 时 auto 的异常行为

我正在使用自动遍历vector(附加代码)。在遍历的同时,我还在后面附加了一些元素。我没想到会得到这样的输出。#include#includeusingnamespacestd;vectordynamic_vector;voidaccess(){for(autoi:dynamic_vector){if(i==3){dynamic_vector.push_back(4);dynamic_vector.push_back(5);}cout输出:123我原以为从1到5的所有数字都会被打印出来。我无法理解如何使用auto进行遍历? 最佳答案

c++ - lambdas 中的模板参数列表中的 auto 是标准的一部分吗?

今天,我偶然发现了以下代码片段:#includeintmain(){autoa=[](std::pairvalue){};a(std::pair{3,true});}http://cpp.sh/5p34我只有一个问题:标准支持这段代码吗?它在GCC中编译(使用-std=c++14),但不是clang或VisualStudio2015(VC++14)。这似乎应该成为标准的一部分,因为如果lambda应该具有与常规函数相同的模板支持,那么应该支持它。这似乎可以转换为所有模板类型,而不仅仅是std::pair。 最佳答案 在C++14中,

c++ - const auto 和 auto const 如何应用于指针?

我尝试了一些代码,想知道在使用auto时C++中的const限定符如何应用于指针类型。intmain(){intfoo=1;intbar=2;//Expected:constint*ptr_to_const_int=&foo;constautoptr_to_const_int=&foo;//Expected:int*constconst_ptr_to_int=&foo;autoconstconst_ptr_to_int=&foo;*ptr_to_const_int=3;//Thoughtthiswoulderror//ptr_to_const_int=&bar;Thisdoeserro

c++ - 使用 `extern template` 防止模板类的隐式实例化

考虑以下代码片段:templatestructX{};externtemplatestructX;intmain(){X{};}它编译并链接:liveexampleongodbolt.org.由于externtemplate声明,我希望它不会链接。我的理解是externtemplate的意思是:“请不要在这个TU中实例化这个特定的模板特化,它将由其他一些TU提供,你可以链接到它”.示例/描述。我在isocpp上看到过,cppreference似乎验证了我的心智模型。例如Fromhttps://en.cppreference.com/w/cpp/language/class_templa

c++ - 使用 `A<int>::template B<int> x;` 定义变量是否符合 C++ 标准?

这个问题在这里已经有了答案:WhereandwhydoIhavetoputthe"template"and"typename"keywords?(8个答案)关闭8年前。gcc、vc++和clang接受以下代码。templatestructA{templatestructB{};};intmain(){A::By;//OKasexpectedA::templateBx;//AlsoOK!Isthisstandard-compliant?};使用A::templateBx;定义变量是否符合C++标准??

c++ - 如何初始化用 auto 关键字声明的循环计数器?

这是我的代码:#include#includevoidcumulative_sum_with_decay(std::vector&v){for(autoi=2;i&v){std::cout{1,2,3,4,5,6,7,8,9,10};cumulative_sum_with_decay(v);printv(v);}当我尝试编译和运行这个程序时,我收到了这些警告:$clang++-std=c++11-Wextrafoo.cpp&&./a.outfoo.cpp:6:24:warning:comparisonofintegersofdifferentsigns:'int'and'std::__

c++ - 使用 "template"编写 C 或 C++ 库

(1)。在使用C++模板时,编译器(如g++)不会直接编译模板定义(只能​​在头文件中,不能在源文件中),而是根据模板定义为它的每个实例生成代码,然后为它的实例编译生成的代码?(2)。如果我想编写一个提供模板类和模板函数的C++库,是否无法将库编译为共享文件(.so,.a),因为它们的实例化不会出现在库代码中的任何位置,而只会出现在用户程序?如果是,是否意味着模板库只是源代码文件而不是预编译文件?C++标准模板库(STL)是如何实现的?其源代码是预编译的还是与用户程序一起编译的?(3)。在C中,如何编写一个库来提供像C++中的模板函数一样的函数?重载是一个好的解决方案吗?如果我必须为不

c++ - 使用 Lambda/Template/SFINAE 自动化 try/catch-safeguarding trampoline 函数

我有100个左右的蹦床功能。我想知道是否可以自动将每个包装在try/catchblock中。请提前警告,这不是一个简单的问题。我将首先用(简化的)代码描述问题,然后在下面尝试尽可能地回答它,以便读者可以看到我的位置。Foo有一个函数指针表:编辑:这是一个C函数指针表。所以它可以接受staticW::w。签名在这里:http://svn.python.org/projects/python/trunk/Include/object.h编辑:我尝试了一个测试用例here:classFoo{Tabletable;Foo(){//Eachslothasadefaultlambda.:table