草庐IT

auto_created

全部标签

c++ - 'auto' 关键字有什么意义?

所以我理解在C#中使用var是有意义的,因为你有编译器派生的匿名类型。C++似乎没有此功能(除非我错了),那么使用auto关键字有什么意义呢?(这有点酷,与C#不同,auto确实适用于成员/全局变量,我想这很酷,但似乎不足以证明它的存在)。 最佳答案 auto归结为通用编程和节省程序员的输入时有很多用途。例如,考虑这个。你愿意输入:std::unique_ptrg=std::make_unique(1,2,3,4)或:autog=std::make_unique(1,2,3,4)是的,它们都很长,但我们知道返回类型并再次指定它输入起

c++ - 使用 auto 重载模板函数的解析

使用以下3个重载templateautofoo(){return1;}templateintfoo(){return2;}templateTfoo(){return3;}以下是病态的吗?static_cast(&foo)();Clang选择重载#2,而gcc编译失败(Demo)当删除重载#1时,双方都同意选择重载#2(Demo)。删除重载#2时,gcc选择重载#1并且clang编译失败(Demo) 最佳答案 根据[over.over]/2,我们执行模板参数推导。这对于所有三个重载都会成功:在第一个重载中,保留[temp.deduct

c++ - C++14 中 decltype(auto) 的转换函数

classA{public:intnum;A(intparam):num(param){}operatordecltype(auto)(){returnnum;}};classB{public:intnum;AobjA;B(intparam):num(param),objA(param){}//operatorA(){returnobjA;}//Works//#1//operatorint(){returnobjA;}//Works//#2//operatorchar(){returnobjA;}//ActuallyNotNeeded//#3//operatordouble(){ret

c++ - 为什么使用 auto 的直接列表初始化被认为是不好的或不受欢迎的?

我已经养成了使用直接列表初始化编写代码的习惯,因为它更有效,而且对于防止隐式narrowing非常有用。:inti{0};strings{""};charc{'a'};boolb{false};autonum{100};//Butthis??但是当涉及到自动说明符时,我听说这样写被认为是不好的或不受欢迎的,这是为什么呢? 最佳答案 这是使用该语法失败的示例:structFoo{};voideatFoo(constFoo&f){}intmain(){Fooa;autob{a};eatFoo(b);}您可能希望这没问题:b应该是Foo并

c++ - 使用 std::iterator traits 和 auto 在函数声明中定义一个函数

今天我尝试实现基数排序。该函数必须有两个变量:开始迭代器和结束迭代器,并且可以有第三个:一些必须返回整数类型以进行排序的函数。默认情况下,它必须是恒等函数。我的尝试看起来像(抱歉,代码看起来又长又脏,但这只是一个尝试):templatevoidradix_sort(ForwardItfirst,ForwardItlast,std::function::value_type)>get_value=[](consttypenamestd::iterator_traits::value_type&x){returnx;}){//...}get_value的返回类型当然会在编译时知道。用法应该

c++ - 谷歌模拟 : Mocked overloaded functions create warning C4373

我正在使用GoogleMock模拟一个具有2个重载函数的C++类和VS2010:#include"stdafx.h"#include"gmock/gmock.h"#include"A.h"classMockA:publicA{public://...MOCK_METHOD3(myFunc,void(constintid,constinterrorCode,constCStringerrorMsg));MOCK_METHOD1(myFunc,void(constCStringerrorMsg));//...};每次编译我都会收到两次以下警告:1>c:\dev\my_project\tes

springboot启动报错Error creating bean with name requestMappingHandlerMapping defined in class path resou

报错信息:org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'requestMappingHandlerMapping'definedinclasspathresource[com/huashang/config/WebMvcConfig.class]:Invocationofinitmethodfailed;nestedexceptionisjava.lang.IllegalStateException:Ambiguousmapping.Cannotmap'projectContr

c++ - 如何将函数参数传递给 boost::thread_groups::create_thread()

我是Boost.Threads的新手,正在尝试了解如何将函数参数传递给boost::thread_groups::create_thread()函数。在阅读了一些教程和boost文档之后,我了解到可以简单地将参数传递给该函数,但我无法使该方法起作用。我读到的另一种方法是使用仿函数将参​​数绑定(bind)到我的函数,但这会创建参数的拷贝,我严格要求传递const引用,因为参数将是大矩阵(我打算这样做通过使用boost::cref(Matrix)一旦我得到这个简单的例子。现在,让我们开始讨论代码:voidprintPower(floatb,floate){cout这不会编译并出现以下错误

C++ typedef 和返回类型 : how to get the compiler to recognize the return type created with typedef?

#includeusingnamespacestd;classA{typedefintmyInt;intk;public:A(inti):k(i){}myIntgetK();};myIntA::getK(){returnk;}intmain(intargc,char*constargv[]){Aa(5);cout在这一行中,myInt未被编译器识别为“int”:myIntA::getK(){returnk;}如何让编译器将myInt识别为int? 最佳答案 typedef创建同义词,而不是新类型,因此myInt和int已经相同。问题

c++ - 为什么 auto_ptr 构造不能使用 = 语法

我遇到了一个对我来说意义不大的编译器错误:#includeusingnamespacestd;auto_ptrtable=db->query("select*fromt");错误:请求从“Table*”到非标量类型“std::auto_ptr”的转换但是,以下行确实有效:auto_ptrtable(db->query("select*fromt"));构造函数的这个定义阻止它按我预期的方式工作的原因是什么?我认为初始化声明使用了构造函数。这是我的auto_ptr的构造函数(来自SGISTL):explicitauto_ptr(element_type*__p=0)throw():_M_