草庐IT

template-parameter-lists

全部标签

c++ - reference_wrapper : make_pair VS Class Template Argument Deduction (CTAD)

为什么make_pair和类模板参数推导(CTAD)不同意生成哪种类型?#include#include#include#includeintmain(){intmyInt=5;std::reference_wrappermyIntRef=myInt;automyPair=std::make_pair(myInt,myIntRef);std::pairMy2ndPair(myInt,myIntRef);std::cout输出:St4pairIiRiE//std::pairSt4pairIiSt17reference_wrapperIiEE//std::pair>更新:为什么std::p

C++ Array of 120 ob​​jects with constructor + parameters, header- + sourcefile, no pointers please!

文件.h:externobjektsquares[120];文件.cpp:objektsquares[120]={objekt(objekt_size,objekt_size,-111,0)};我怎样才能一次初始化所有对象,所有对象都使用相同的参数? 最佳答案 不要使用原始数组(因为所有元素都将通过默认构造函数初始化)。使用例如一个std::vector:std::vectorsquares(120,objekt(objekt_size,objekt_size,-111,0)); 关于C

c++ - 嵌套模板特化结果为 "Illegal use of explicit template arguments"?

如何专门化嵌套模板?(请参阅下面的错误。)usingstd::reverse_iterator;templatereverse_iteratormake_reverse_iterator(constIt&it){returnreverse_iterator(it);}templateItmake_reverse_iterator>(constreverse_iterator&it){//Above^//errorC2768://'make_reverse_iterator':illegaluseofexplicittemplateargumentsreturnit.base();}

c++ - 为什么 std::list 有最大尺寸?

如果std::list是一个链表,那为什么要限制元素的数量?每个元素都是指向新节点的链接,您可以拥有多少个指针没有限制。 最佳答案 Ifstd::listisalinkedlistthenwhyistherealimitonhowmanyelementsyoucanhave?因为max_size()函数是所有标准容器的要求。Eachelementisalinktoanewnodeandthere'snolimitonhowmanypointersyoucanhave.是的:大小必须由size_type表示,因此限制是该类型的最大值。

c++ - 从 initializer_list 错误构造 std::map

我正在尝试创建一个类构造函数,它将接受一个初始化列表并用它初始化一个映射,如下所示:classTest{std::mapm_ints;public:Test(std::initializer_list>init):m_ints(init){}};但这会导致很长的错误消息,坦率地说,我不明白。我需要更改什么才能使这项工作正常进行? 最佳答案 声明std::initializer_list的模板参数具有类型std::pair这是一个演示程序#include#include#includeclassTest{std::mapm_ints;

c++ - 在每个源文件中替代 "extern template"

我正在开发一个库,其中我们的许多核心对象都是模板,其中一个特定实例以指向该模板实例的智能指针的形式出现在项目的大多数文件中。我在单个源文件中明确实例化了这些模板。我们最近切换到C++11,我正在尝试使用新的externtemplateclassMyTemplate;加快编译速度。我的第一个问题是我是否在周围使用智能指针MyTemplate正在隐式实例化模板并要求文件顶部的“外部模板..”以避免重复实例化。我的第二个问题是是否有一些替代方法来添加所有这些externtemplateclassMyTemplate;到每个源文件。为我定义的每个模板搜索智能指针的每个实例并确保我在该文件中有正

C++ : Check if the template type is one of the variadic template types

这个问题在这里已经有了答案:Checkifatypeispassedinvariadictemplateparameterpack(3个答案)关闭7年前。假设我们有函数:templatevoidfoo(){...};检查“Kind”类型是否是C++(包括C++1z)中的“Kinds”类型之一的最简单方法是什么?

c++ - 有没有办法交换 std::list 中的节点?

我正在实现LRUCache,我在unordered_map中存储了一个要列出的迭代器。当我将最“新鲜”的元素移动到头部时,我需要迭代器不变。我需要准确交换节点,而不是节点中的值。我正在寻找实现它的方法。我尝试用std::iter_swap来实现,但它只是实现为std::swap(*it_first,*it_second)std::listlist;list.emplace_back("first");list.emplace_back("second");autoit_first=list.begin();autoit_second=++list.begin();std::iter_s

c++ - std::map.insert "could not deduce template argument for..."

我正在尝试熟悉STL库,但我无法理解我的编译错误。我使用编译器错误字符串“无法推断...的模板参数”搜索了其他问题,但没有一个答案似乎适用或相关。Error4errorC2784:'boolstd::operator&,conststd::unique_ptr&)':couldnotdeducetemplateargumentfor'conststd::unique_ptr&'from'conststd::string'c:\programfiles(x86)\microsoftvisualstudio10.0\vc\include\xfunctional125我正在编写一个简单的解释

c++ - 为什么 std::map 没有 find/lower_bound 重载,std::list 没有 sort 重载?

我知道你不应该使用std::find(some_map.begin(),some_map.end())或std::lower_bound,因为它会采用线性时间而不是some_map.lower_bound提供的对数时间。std::list也会发生类似的事情:有用于排序的std::list::sort函数,但您无法调用std::sort(some_list.begin(),some_list.end()),因为迭代器不是随机访问的。但是,例如,std::swap具有标准容器的重载,因此swap(some_map,other_map)的调用需要O(1),而不是在)。为什么C++标准不为ma