草庐IT

template-auto

全部标签

c++ - 我们是否应该始终对局部变量使用 auto&&

阅读此主题后auto&&,这是否意味着我们在声明局部变量以捕获函数的返回类型时应该始终使用auto&&而不是auto(以准确保留功能)?用例可以是例如auto&&result=func_returning_lvalue_or_lvalue_reference();或auto&&iterator=vector_.begin();或其他任何内容。换句话说,有很多auto&&的基本代码是正常的吗? 最佳答案 不。您不应该一直使用auto&&。特别是,如果您需要拷贝,则不应使用它。使用auto&&,您可能会获得一个拷贝(作为对临时对象的引用

c++ - 这是一个很好的 std::auto_ptr<> 用例吗?

请假设我有一个接受指针作为参数的函数。这个函数可以抛出异常,因为它使用std::vector::push_back()管理此指针的生命周期。如果我这样声明:voidmanage(T*ptr);并这样称呼它:manage(newT());如果它抛出异常将指针插入std::vector,我实际上有内存泄漏,不是吗?会像这样声明函数:voidmanage(std::auto_ptrptr);解决我的问题?我希望它首先分配std::auto_ptr在堆栈上(我猜永远不会抛出异常的东西)并让它获得对指针的所有权。安全的。然后,在函数内部,我将原始指针插入std::vector,这也是安全的:如果

c++ - 在 std::auto_ptr 的构造函数中使用 `explicit` 关键字有什么原因吗?

这是用于在VS2008编译器中从标准指针构造std::auto_ptr对象的构造函数。templateclassauto_ptr{public:explicitauto_ptr(_Ty*_Ptr=0)_THROW0():_Myptr(_Ptr){}private:_Ty*_Myptr;};explicit是否有任何特殊原因?上面使用关键字?换句话说,为什么我不能初始化auto_ptr与std::auto_ptrptr=newClassA;? 最佳答案 因为否则您可能会无意中执行以下操作:voidfoo(std::auto_ptrp)

c++ - g++ 可变参数模板。简单示例代码无法编译,提示 'Not a template'

我正在探索这个陌生的领域,并想尝试来自DannyKalev'stutorialonthematter的一个简单示例.代码非常简单:templatestructCount{staticconstintvalue=0;};templatestructCount//partialspecialization{staticconstintvalue=1+Count::value;};但是gcc4.4.7甚至4.7.0提示(尽管-std=c++0x-std=gnu++0x标志):/src/tests/VTemplates.h:12:8:error:'Count'isnotatemplate/sr

c++ - 在多线程环境中使用 auto_ptr 交换对象而不锁定是安全的吗?

我在堆上分配了一些数据结构,它们很少被修改但需要快速读取访问。一个例子是在堆上分配的结构,许多线程以只读方式非常频繁地访问它。需要定期重写此结构并避免锁定争用我想知道使用auto_ptr是否安全基本上允许已获取引用的线程继续处理直到它们完成writer创建结构的拷贝,重写它并快速与结构的新auto_ptr实例交换指针。我从Java中的CopyOnWriteArrayList得到这个想法,并希望在C++中执行类似的性能。 最佳答案 std::auto_ptr在调用非常量成员(例如reset())时没有任何线程安全保证正如你所建议的。此

c++ - 错误 C2899 : typename cannot be used outside a template declaration

我正在MSV2010中尝试以下内容namespacestatismo{templatestructRepresenterTraits,3u>>{typedefitk::Image,3u>VectorImageType;typedefVectorImageType::PointerDatasetPointerType;typedefVectorImageType::PointerDatasetConstPointerType;typedeftypenameVectorImageType::PointTypePointType;typedeftypenameVectorImageType:

c++ - 使用 constexpr auto/char-array 变量观察到的不同行为

跟进这个问题Havingaconstexprstaticstringgivesalinkererror在问题中,这段代码无法编译:#includestructTest{staticconstexprchartext[]="Text";};intmain(){std::cout从评论来看,这段代码是可以编译的:#includestructTest{staticconstexprautotext="Text";};intmain(){std::cout我的问题是,为什么auto版本可以工作,而char版本的数组却不行?能否请您指出标准中允许使用第二个版本而不允许使用第一个版本的声明?我看了

c++ - (C++14) lambda 数组 : error: 'name' declared as array of 'auto'

我很难解决这个错误。我承认,我是C++的新手,我的困难来自于不理解错误消息。代码如下:autoselectionFuncs[8]={[&](constVector3&min,constVector3&max){returnmax.x_==seamValues.x_||max.y_==seamValues.y_||max.z_==seamValues.z_;},[&](constVector3&min,constVector3&max){returnmin.x_==seamValues.x_;},[&](constVector3&min,constVector3&max){returnm

C++ : friend declaration ‘declares a non-template function

我在重载时遇到问题流运算符(operator),我找不到解决方案:templateclassNVector{inlinefriendstd::ostream&operator&rhs);};templateinlinestd::ostream&NVector::operator&rhs){/*SOMETHING*/returnlhs;};它产生以下错误信息:warning:frienddeclaration‘std::ostream&operatorerror:‘std::ostream&NVector::operator如何解决这个问题?非常感谢。 最佳答

c++ - 错误 : expected primary-expression before ‘>’ : templated function that try to uses a template method of the class for which is templated

这个问题在这里已经有了答案:WhereandwhydoIhavetoputthe"template"and"typename"keywords?(8个答案)关闭8年前。在使用模板和仿函数(未出现在这个问题中)时,我最终遇到了以下简化的问题。以下代码(也可用here)classA{public:templateboolisGood(intin)const{constTf;returninbooltryEvaluator(T&evaluator,intvalue){returnevaluator.isGood(value);}intmain(intargc,constchar*argv[]