草庐IT

static_if

全部标签

c++ - 是否有可能 static_assert lambda 不是通用的?

我实现了一个Visit函数(在变体上),它检查变体中当前事件的类型是否与函数签名(更准确地说是第一个参数)匹配。基于这个不错answer.例如#include#include#includetemplateArgfirst_argument_helper(Ret(*)(Arg,Rest...));templateArgfirst_argument_helper(Ret(F::*)(Arg,Rest...));templateArgfirst_argument_helper(Ret(F::*)(Arg,Rest...)const);templatedecltype(first_argum

c++ - 以 enable_if 作为模板参数的模板类方法定义

我问了thisquestion早些时候在哪里asolution被提出。就问题而言,解决方案很棒,但现在我对如何定义类的方法outside感到困惑,即我想在.inl中定义方法文件。这种情况下的语法是什么?明确一点,对于模板类,方法定义为:templatestructFoo{Foo();};//C-tordefinitiontemplateFoo::Foo(){}我如何为模板类定义方法,将enable_if作为参数之一?template::value>::type>structFoo{ Foo();};//C-tordefinition--??? 最佳答案

c++ - 在 C++ 中使用 "else if"

我有两个问题——(一)code-fragment-1if(){}elseif(){//statements-1}//statements-2代码片段2if(){}else{if(){//statements-1}//statements-2}以上两段代码是否相同?(II)什么时候使用elseif(在C++中)? 最佳答案 唯一的区别是在示例1中,无论您检查的条件如何,您的Statement2都会执行。在示例2中,Statement2只有在您的if条件为假时才会执行。除此之外,它们基本相同。

c++ - static_cast 可以在 C++ 中抛出异常吗?

假设static_cast永远不会抛出异常是否安全?对于int到Enum的转换,即使无效也不会抛出异常。我可以依赖这种行为吗?以下代码有效。enumanimal{CAT=1,DOG=2};inty=10;animalx=static_cast(y); 最佳答案 对于这种特定类型的转换(枚举类型的组成部分),可能会抛出异常。C++standard5.2.9Staticcast[expr.static.cast]paragraph7Avalueofintegralorenumerationtypecanbeexplicitlyconve

c++ - 在结构/类中使用 static const int

structA{staticconstinta=5;structB{staticconstintb=a;};};intmain(){returnA::B::b;}上面的代码可以编译。但是,如果您阅读ScottMyers的《EffectiveC++》一书(第14页);除了声明之外,我们还需要a的定义。谁能解释为什么这是一个异常(exception)? 最佳答案 C++编译器允许staticconst整数(并且仅限于整数)在它们声明的位置指定它们的值。这是因为基本上不需要该变量,它只存在于代码中(通常是编译出来的)。其他变量类型(例如s

c++ - 我怎么说 "noexcept if execution of protected base constructor is noexcept"?

我们遇到过这种情况,想知道解决它的最佳方法templatestructA:T{A(T&&t)noexcept(noexcept(T(std::move(t)))):T(std::move(t)){}};不幸的是编译失败,因为T的移动构造函数是protected,我们只能在*this的构造函数初始化列表中调用它。使这项工作有什么变通办法,或者甚至有标准的方法吗? 最佳答案 您正在寻找noexcept(std::is_nothrow_move_constructible::value):http://en.cppreference.co

c++ - 在编译时在 static_assert() 中显示整数

这是我正在尝试做的简化版本enumFirst{a,b,c,nbElementFirstEnum,};enumSecond{a,b,c,nbElementSecondEnum,};static_assert(First::nbElementFirstEnum==Second::nbElementSecondEnum,"Notthesamenumberofelementintheenums.");/*static_assert(First::nbElementFirstEnum==Second::nbElementSecondEnum,"Notthesamenumberofelementi

c++ - 为什么 std::copy_if 签名不约束谓词类型

假设我们有以下情况:structA{inti;};structB{Aa;intother_things;};boolpredicate(constA&a){returna.i>123;}boolpredicate(constB&b){returnpredicate(b.a);}intmain(){std::vectora_source;std::vectorb_source;std::vectora_target;std::vectorb_target;std::copy_if(a_source.begin(),a_source.end(),std::back_inserter(a_t

c++ - 类似于 "if constexpr"但用于类定义

ifconstexpr是在C++程序中摆脱预处理器的一大步。然而,它仅适用于函数-如本例所示:enumclassOS{Linux,MacOs,MsWindows,Unknown};#ifdefined(__APPLE__)constexprOSos=OS::MacOs;#elifdefined(__MINGW32__)constexprOSos=OS::MsWindows;#elifdefined(__linux__)constexprOSos=OS::Linux;#elseconstexprOSos=OS::Unknown;#endifvoidprintSystem(){ifcons

c++ - 为什么标准库有find和find_if?

难道find_if不能只是find的重载吗?std::binary_search和friend就是这样做的... 最佳答案 谓词是一个有效的查找对象,因此您可能会产生歧义。考虑将find_if重命名为find,那么你有:templateInputIteratorfind(InputIteratorfirst,InputIteratorlast,constT&value);templateInputIteratorfind(InputIteratorfirst,InputIteratorlast,Predicatepred);然后,应