草庐IT

decltype-auto

全部标签

c++ - 混合 decltype 和 enable_if

似乎将decltype与SFINAEenable_if一起使用并不简单。我尝试以三种不同的方式使用enable_if编写go。所有这些都因编译器错误而失败(GCC的字面意思是:“错误:'thing'不是'foo'的成员”和实例化上下文)。#includestructfoo{enum{has_thing=false};};structbar{enum{has_thing=true};staticintthing(){return0;}};templatestructTest{/*autogo(typenamestd::enable_if::type=0)->decltype(T::thi

c++ - `auto` 之前是做什么用的?

我知道在C++11之前,auto关键字具有完全不同的含义;它是一个存储类型说明符,指示具有自动存储类型(即,放置在堆栈上)的对象。理论就是这样……您实际上会如何使用此关键字(语法),为什么?另外,我还没有在C++11之前的实际代码中看到过这个关键字;它什么时候有用(什么时间段)? 最佳答案 它用于声明一个具有自动存储持续时间(即“在堆栈上”)的局部变量。至少自C90以来,它一直是一个无用的关键字,因为自动存储持续时间是局部变量的默认存储持续时间。 关于c++-`auto`之前是做什么用的

c++ - decltype(constexpr 变量)

为什么constexpr变量的decltype失败?#include#includeconstexpruint16_tfoo(){return0;}constexprautocv=foo();autov=foo();static_assert(std::is_same::value,"!");//failedstatic_assert(std::is_same::value,"!");//success 最佳答案 decltype(entity)指定此表达式指定的实体的声明类型。由于constexpr,(对象声明中使用的conste

c++ - const auto * 和 const auto 之间的区别?

我有这个代码:constinta=10;constauto*b=&a;//0x9ffe34constautoc=&a;//0x9ffe34intz=20;b=&z;//0x9ffe38//c=&z;//[Error]assignmentofread-onlyvariable'c'为什么可以将新地址分配给b而不是分配给c? 最佳答案 b将被推导出为constint*,这意味着一个指向constint的非常量指针>,所以改变b本身的值就可以了。c将被推导出为constint*const,这意味着一个const指针指向constint,所

c++ - 当 auto 遇到多态和虚函数时,正确的行为是什么?

classB{public:virtualvoidf(){printf("B\n");}};classD:publicB{public:voidf(){printf("D\n");}};intmain(void){B*d=newD();d->f();autob=*d;b.f();}对于d->f();,输出是D。这是正确的。但是对于b.f();,输出是B。这样对吗? 最佳答案 Isthisright?没错,类型是在编译时推导的。auto使用与templateargumentdeduction相同的规则对于类型推导,基于静态类型,不考虑

c++ - 什么时候需要在 C++ 中使用 decltype

我正在学习C++,并且在C++primer中找到了以下代码:intmain(){strings("somestring");for(decltype(s.size())index=0;index!=s.size()&&!isspace(s[index]);++index)s[index]=toupper(s[index]);cout我理解代码将字符串中的第一个单词大写,输出是SOMEstring我很好奇的是为什么你需要使用decltype(s.size())来声明索引的类型。如果我将其更改为intindex=0,代码仍然可以正常编译和运行。对我来说使用int似乎更容易。我想我在这里遗漏

c++ auto 多选

在我的项目冒险过程中,我意识到如果我想根据条件初始化参数,我不能利用新的c++11auto关键字的优势。基本上我有这样一个代码片段:autofoo=bar::getfoo();需要更改为:FOOfooif(cond){foo=bar::getfoo();}else{foo=baz::getotherfoo();}但是我需要用类型声明foo(因为编译器不知道我将使用相同的类型返回。我想知道在这种情况下是否有任何使用auto关键字的方法。我想出的另一个解决方案是使用?:具有这样代码的运算符:autofoo=cond?bar::getfoo():baz::getotherfoo();但是如果

c++ - std::auto_ptr、delete[] 和泄漏

为什么这段代码不会导致内存泄漏?intiterCount=1000;intsizeBig=100000;for(inti=0;ibuffer(newchar[sizeBig]);}WinXPsp2,编译器:BCB.05.03 最佳答案 因为你(不)幸运。auto_ptr调用delete,而不是delete[]。这是未定义的行为。尝试做这样的事情,看看你是否幸运:structFoo{char*bar;Foo(void):bar(newchar[100]){}~Foo(void){delete[]bar;}}intiterCount=1

c++ - auto 的编译器问题?错误 : in a declarator-list 'auto' must always deduce to the same type

std::vectorvec;autoi=vec.begin(),j=std::next(i);Error:inadeclarator-list'auto'mustalwaysdeducetothesametype 最佳答案 在Linux上的g++中编译良好,因此它似乎是一个编译器错误。Probablythisone. 关于c++-auto的编译器问题?错误:inadeclarator-list'auto'mustalwaysdeducetothesametype,我们在StackOve

linux使用grep命令查询nginx的进程情况时总是出现 grep --color=auto nginx

问题:每次使用psaux|grep服务名命令查询某个服务的进程时,总会出现一条grep--color=auto服务名例如:psaux|grepnginx#会出现图片中的情况解答:这是因为grep也是一条命令,它在输出时,会把grep服务名也当做一个进程输出,假如使用grep命令查询某个服务的进程号,结果只显示一条grep--color=auto服务名。则说明虚拟机中没有改服务的进程。ChatGPT解答:因为psaux命令会列出当前系统中所有的进程信息,而grepnginx是用于筛选出包含“nginx”关键字的行。由于grep命令本身也被包括在进程列表中,所以它也会被psaux命令找到并显示出来