草庐IT

static_if

全部标签

c++ - 将函数内的大变量声明为 `static` 在性能上有什么不同吗?

不确定之前是否有人问过这个问题。在回答thisverysimplequestion时,我问自己以下内容。考虑一下:voidfoo(){inti{};constReallyAnyType[]data={item1,item2,item3,/*manyitemsthatmaybepotentiallyheavytorecreate,e.g.ofclasstype*/};/*functioncodehere...*/}现在理论上,每次控制达到功能时都会重新创建局部变量,对吗?IE。看看上面的inti-它肯定会在堆栈上重新创建。上面的数组呢?编译器能否聪明到优化它的创建只发生一次,还是我在这里

C++ - 为什么程序在映射迭代器中使用 if 语句崩溃?

我是C++的新手,我试图在传递if语句的同时遍历映射。但是程序崩溃了。请帮我修复程序。#include#include#include#include#includeusingnamespacestd;intmain(){std::maph;std::map::iteratorit;h[1]=2;h[4]=5;for(it=h.begin();it!=h.end();it++){if(it->second>4){h.erase(it->first);}} 最佳答案 您正在删除for循环内的元素,指向已删除元素(即it)的迭代器将失效

c++ - 如何 static_assert 给定的函数调用表达式是否可以编译?

有没有一种方法可以检查函数调用表达式是否会在编译时进行编译并对其进行static_assert?还是我应该通过system()调用编译器并检查退出代码?#includetemplatevoidf(Args&&...args,bool);templatevoidg(Args&&...args);intmain(){g(1,2.0,"hello",false);//compilesf(1,2.0,"hello",false);//doesn'tcompile//HowdoIdothis?//static_assert(!does_this_compile(f(1,2.0,"hello",f

c++ - constexpr 递归函数是否使用 if constexpr

使用gcc(HEAD7.0.0201612)我惊讶地发现这有效:constexprlongvalue(constchar*definition){if(definition&&*definition){return*definition+value(definition+1);}return*definition;}intmain(){longl{};std::cin>>l;switch(l){casevalue("AAAA"):f1();break;casevalue("BBBB"):f2();break;default:error();break;}return0;}文字字符串"A

c++ - enable_if 检查迭代器的值类型是否是一对

我想为值类型为一对的迭代器编写一个专门的模板函数。我的期望是这应该匹配std::map的迭代器。为了检测对:templatestructis_pair:std::false_type{};templatestructis_pair>:std::true_type{};//alsotriedthis,butitdidn'thelptemplatestructis_pair>:std::true_type{};然后我在函数声明中使用enable_if:templatedecltype(auto)do_stuff(std::enable_if::value,ITR>itr){//access

c++ - 使用 "if constexpr"防止元组越界

以下代码在GCC和Clang下编译良好,但在VisualStudio(/std:c++latest)的最新更新中停止工作:#includetemplatevoidcheck_tuple(T...types){ifconstexpr(pos>::type;}}intmain(){check_tuple(1.0,1.0);check_tuple(1.0,1.0);}在最新版本的VisualStudio(/std:c++latest)中,编译失败,元组索引越界(std::tuple_element>)。是否可以像这样使用constexpr来防止元组越界? 最佳答案

c++ - static_cast 和 dynamic_cast 在特定场景中的不同行为

在下面的场景中,我没有弄清楚static_cast和dynamic_cast之间的真正区别:**///withstatic_cast///**classFoo{};classBar:publicFoo{public:voidfunc(){return;}};intmain(intargc,char**argv){Foo*f=newFoo;Bar*b=static_cast(f);b->func();return0;}Output:SuccessfullyBuildandCompiled!**///withdynamic_cast///**classFoo{};classBar:publ

c++ - 使用 enable_if 的模板特化在 Clang 中失败,适用于 GCC

我正在尝试删除基于模板类型的成员函数。问题是在未删除的情况下,使以后的模板特化与我的函数的类型签名匹配。我尝试了以下代码,它使用GCC(9.0.1)编译,但在Clang(9.0.0)中出错。我认为它也无法在MSVC++中构建代码。#include#includetemplatestructmy_type{templatestd::enable_if_t::value,my_type>my_fun(constmy_type&v){std::couttemplatestd::enable_if_t::value,my_type>my_type::my_fun(constmy_type&v)

c++ - `enable_if` 与 `enum` 模板特化问题

我在GCC编译时遇到问题enable_ifs应用于模板类方法的返回值。使用Clang,我可以在enable_if中使用表达式在enum上模板参数,而GCC拒绝编译此代码。这里是问题描述、初始代码及其后续修改,试图让我和编译器满意(不幸的是,不是同时)。我有一个非模板类Logic包含模板化类方法computeThings()它有一个enumStrategy作为其模板参数的之一。computeThings()中的逻辑取决于编译时间Strategy,所以ifconstexpr是一种合理的实现方式。变体1#includeclassLogic{public:enumStrategy{strat_

c++ - sqlite3_bind_text SQLITE_STATIC vs SQLITE_TRANSIENT for c++ string

我有一个返回c++std::string的方法,然后在将其传递到sqlite3_bind_text之前将其转换为c_str()。我的问题是,这应该使用SQLITE_STATIC还是SQLITE_TRANSIENT?sqlite3_bind_text(insertStatement,0,suspect->GetIpString().c_str(),-1,SQLITE_STATIC);//Dosomestuffinsamefunctionthensqlite3_stepsqlite3_bind_text的文档说,Thefifthargumenttosqlite3_bind_blob(),s