草庐IT

if-cases

全部标签

c++ - 为什么模板参数中的 enable_if_t 提示重新定义?

我有以下使用std::enable_if的案例:template::value>::type*=nullptr>voidf(){}template::value>::type*=nullptr>voidf(){}现在,我在cppreference中看到了新语法,在我看来更简洁:typename=std::enable_if_t::value>>我想移植我的代码:template::value>>voidg(){}template::value>>voidg(){}但现在GCC(5.2)提示:error:redefinitionof'templatevoidg()'voidg(){}为什

c++ - 为什么模板参数中的 enable_if_t 提示重新定义?

我有以下使用std::enable_if的案例:template::value>::type*=nullptr>voidf(){}template::value>::type*=nullptr>voidf(){}现在,我在cppreference中看到了新语法,在我看来更简洁:typename=std::enable_if_t::value>>我想移植我的代码:template::value>>voidg(){}template::value>>voidg(){}但现在GCC(5.2)提示:error:redefinitionof'templatevoidg()'voidg(){}为什

c++ - 如何断言 constexpr if else 子句永远不会发生?

如果条件为真,我想在非constexpr时引发编译时错误,例如:ifconstexpr(condition1){...}elseifconstexpr(condition2){....}elseifconstexpr(condition3){....}else{//Iwanttheelseclausenevertaken.ButIheardthecodebelowisnotallowedstatic_assert(false);}//I'drathernotrepeattheconditionsagainlikethis:static_assert(condition1||condit

c++ - 如何断言 constexpr if else 子句永远不会发生?

如果条件为真,我想在非constexpr时引发编译时错误,例如:ifconstexpr(condition1){...}elseifconstexpr(condition2){....}elseifconstexpr(condition3){....}else{//Iwanttheelseclausenevertaken.ButIheardthecodebelowisnotallowedstatic_assert(false);}//I'drathernotrepeattheconditionsagainlikethis:static_assert(condition1||condit

c++ - 为什么用 enable_if 编译错误

为什么这不能用gcc48和clang32编译?#includetemplatestructS{templatetypenamestd::enable_if::typef(Tt){return1;};templatetypenamestd::enable_if::typef(Tt){return2;};};intmain(){Ss1;returns1.f(99);}GCC错误:/home/lvv/p/sto/test/t.cc:12:2:error:notypenamed‘type’in‘structenable_if’f(Tt){return2;};^CLANG错误:/home/lvv

c++ - 为什么用 enable_if 编译错误

为什么这不能用gcc48和clang32编译?#includetemplatestructS{templatetypenamestd::enable_if::typef(Tt){return1;};templatetypenamestd::enable_if::typef(Tt){return2;};};intmain(){Ss1;returns1.f(99);}GCC错误:/home/lvv/p/sto/test/t.cc:12:2:error:notypenamed‘type’in‘structenable_if’f(Tt){return2;};^CLANG错误:/home/lvv

对象 vector 上的 C++ remove_if

我有一个对象vector(顺序很重要)(我们称它们为myobj类),我试图一次删除多个对象。classvectorList{vectormyList;};classmyobj{char*myName;intindex;boolm_bMarkedDelete;}我认为最好的方法是标记特定的myobj对象以进行删除,然后在vector上调用myList.remove_if()。但是,我不确定如何为此使用谓词等。我是否应该在对象中创建一个成员变量,让我说我想删除myobj,然后创建一个谓词来检查是否设置了成员变量?如何将谓词实现为vectorList类的一部分?

对象 vector 上的 C++ remove_if

我有一个对象vector(顺序很重要)(我们称它们为myobj类),我试图一次删除多个对象。classvectorList{vectormyList;};classmyobj{char*myName;intindex;boolm_bMarkedDelete;}我认为最好的方法是标记特定的myobj对象以进行删除,然后在vector上调用myList.remove_if()。但是,我不确定如何为此使用谓词等。我是否应该在对象中创建一个成员变量,让我说我想删除myobj,然后创建一个谓词来检查是否设置了成员变量?如何将谓词实现为vectorList类的一部分?

c++ - "if"条件下的变量赋值

我最近刚刚花了一些时间来找出我的代码中的一个由拼写错误引起的错误:if(a=b)代替:if(a==b)我想知道是否有任何特殊情况需要在if语句中为变量赋值,或者如果没有,为什么编译器不抛出警告或错误? 最佳答案 if(Derived*derived=dynamic_cast(base)){//dostuffwith`derived`}虽然这经常被称为反模式(“使用虚拟调度!”),但有时Derived类型具有Base根本没有的功能(并且,因此,不同的功能),这是打开语义差异的好方法。 关于

c++ - "if"条件下的变量赋值

我最近刚刚花了一些时间来找出我的代码中的一个由拼写错误引起的错误:if(a=b)代替:if(a==b)我想知道是否有任何特殊情况需要在if语句中为变量赋值,或者如果没有,为什么编译器不抛出警告或错误? 最佳答案 if(Derived*derived=dynamic_cast(base)){//dostuffwith`derived`}虽然这经常被称为反模式(“使用虚拟调度!”),但有时Derived类型具有Base根本没有的功能(并且,因此,不同的功能),这是打开语义差异的好方法。 关于