草庐IT

static_if

全部标签

c++ - 如何将 `boost::static_visitor` 实例传递给函数

我正在使用boost::variant在我的项目中经常出现。我的同事们现在想出了传递特定boost::static_visitor实例的想法。以自定义访问类型。她有一些代码如下:#include#includetypedefboost::variantTVar;structVisitor1:publicboost::static_visitor{templateresult_typeoperator()(constT&){return42;}};structVisitor2:publicboost::static_visitor{templateresult_typeoperator(

c++ - 使用 std::enable_if 保护复制构造函数

我编写了一个类来促进具有以下构造函数的类型删除:classEnvelope{public:Envelope(){}templateEnvelope(Runnablerunnable):m_runFunc(&Envelope::RunAndDeleteRunnable),m_runnable(newRunnable(runnable)){}templateEnvelope(Runnable*runnable):m_runFunc(&Envelope::RunRunnable),m_runnable(runnable){}};我想重写第一个非默认构造函数以获取引用而不是值(Runnable

c++ - 如何优化这个 find_if 代码?

我有检查字符串是否只包含字母数字和下划线字符的功能......inlineboolIsValidChar(charx){return(isalnum(x)||(x=='_'));}我的find_if代码是:if(find_if(str.begin(),str.end(),IsValidChar)!=str.end()){...}我只想删除IsValidChar函数并直接将它的内容放在代码的find_if行中。 最佳答案 您基本上是在寻找C++0xlambdaexpressions:if(find_if(str.begin(),str

c++ - C++中static的含义

我认为我的C++相当不错,事实证明我不是。我之前问过一个问题:C++constlvaluereferences其中一个答案中有以下代码:#includeusingnamespacestd;int&GenX(boolreset){staticint*x=newint;*x=100;if(reset){deletex;x=newint;*x=200;}return*x;}classYStore{public:YStore(int&x);int&getX(){returnmy_x;}private:int&my_x;};YStore::YStore(int&x):my_x(x){}intma

c++ - C/C++ : How does this inline if get parsed?

考虑这段代码:intmain(){cout它的输出将是1,而不是Yes或No。为什么将true发送到输出流而不是Yes或No字符串?内联if的其余部分如何解析? 最佳答案 这与操作顺序有关。这与:(coutcout返回ostream&,它必须转换为bool或等价物。?:的结果被扔掉了。如果这看起来很奇怪(为什么这个优先级?),请记住ostream的operator是C++代码中引入的重载,它不允许更改优先级。的优先级专为对移位有意义的内容而设计。它作为流媒体运营商的使用要晚得多。编辑:可能转换为(void*)使用这个:http://

c++ - C++中的多个if语句

我正在做第一个项目欧拉问题,我刚刚做了这个#includeusingnamespacestd;intmain(){intthrees=0;intfives=0;intboth=0;for(inti=0;i我的教授最近纠正了我在另一个问题中这样做的问题,他说了一些关于else语句的内容,但我不明白为什么我必须在下一个if前面添加else。对于它的值(value),我有另一个版本elseif(i%5){fives+=....}他们都工作并为我提供了正确的答案。我的问题是这种思维方式本质上有什么问题,是风格上的问题还是我没有从逻辑上思考某些事情?如果可行,为什么还要使用switch语句?

C++ 初学者 : what is the point of using a variable by reference if using "const"?

我想知道这个函数声明中的逻辑:CMyException(conststd::string&Libelle=std::string(),...按引用使用变量有什么意义?通常,只要变量可能在内部被修改,您就会通过引用传递一个变量...因此,如果您使用关键字const,这意味着它永远不会被修改。这是矛盾的。谁能给我解释一下? 最佳答案 实际上引用是用来避免不必要的对象拷贝。现在,要理解为什么使用const,试试这个:std::string&x=std::string();//error编译会报错。这是因为表达式std::string()创

c++11:为什么 std::forward 中的 static_assert 是必需的?

在move.h中,forward有两个重载templateconstexpr_Tp&&forward(typenamestd::remove_reference::type&__t)noexcept{returnstatic_cast(__t);}templateconstexpr_Tp&&forward(typenamestd::remove_reference::type&&__t)noexcept{static_assert(!std::is_lvalue_reference::value,"templateargumentsubstituting_Tpisanlvalueref

c++ - 使用 static_assert 确定特定模板参数是否为特定无类型类模板

我想要一个将参数限制为仅派生自特定模板类的类型的函数。在这种情况下,basic_string(来自STL-docs)。例如,声明了一个wstring:typedefbasic_string,allocator>wstring;基本思路是这样的:templatevoidstrings_only_please(TStringmessage){static_assert(is_base_of::value,"Notastringtype!");}当然,虽然没有指定basic_string,但它无法编译……它需要一个真实的类型。(虽然我可能只对少数实际字符串类型进行硬编码,但我正在寻找针对此模

c++ - 使用 enable_if 选择特征 - 适用于 clang,但不适用于 gcc

我正在编写一段通用的(C++11)代码,它应该与boost::multi_array、Eigen::Matrix一起工作,并且可能其他类型的n维数组。在几个点上,我需要访问给定数组类型的元素类型。boost数组包含一个名为Element的typedef,而Eigen数组包含一个名为Scalar的typedef。我想要的是一个返回给定数组类型的元素类型的类型特征。不幸的是,我不能只为所有可能的数组类型模板专门化特征类,因为Eigen使用表达式模板,因此,有无限多种类型的Eigen矩阵。因此,我将SFINAE与enable_if一起使用来实现我的特征。enable_if应该选择的标准是,一