草庐IT

if-constexpr

全部标签

带有 std::enable_if 的 C++ 可变参数模板部分模板特化

ITNOA我的问题是如何在可变参数模板部分模板特化场景中使用std::enable_if?例如,我有一个类使用如下所示的可变参数模板部分特化/***Commoncase.*/templatestructfoo;/***Finalsuperclassforfoo.*/templatestructfoo{voidfunc(){}};/***Regularfooclass.*/templatestructfoo:publicfoo{typedefsuperfoo;voidfunc(){coutsuper::templatefunc();}}它工作正常,但如果H是整数类型,我想要特定的部分特化

c++ - __LINE__ 在 MSVC 中不是 constexpr

2022年11月28日更新Microsoft(R)C/C++OptimizingCompilerVersion19.34.31933forx86这个问题似乎已经解决了。原帖好的,最新的VS2019社区,本地“所有默认”C++控制台项目:intmain(){//clVersion19.21.27702.2forx86//constexprautoMSCVER=_MSC_VER;//1921constexprautoMSCFULLVER=_MSC_FULL_VER;//192127702constexprautoMSCBUILD=_MSC_BUILD;//2/*:errorC2131:ex

C++中的constexpr和inline有何区别与联系?

在C++编程中,constexpr和inline是两个常被用于函数声明的关键字,它们分别有不同的语义和用途,但也可以在某些情况下结合使用。本文将深入探讨constexpr和inline在C++中的区别与联系,以及它们如何影响程序的性能和编译过程。一、constexpr关键字constexpr是C++11引入的一个关键字,用于指定表达式或对象的值在编译时就可以确定。当一个变量或函数被声明为constexpr时,编译器会尝试在编译阶段计算其值,以确保它可以在程序运行时作为一个常量使用。例如:constexprinta=5;//a是一个编译时常量constexprintb=a*2;//b同样是一个编

C++11与C++14中constexpr的变革

在现代C++编程中,constexpr是一个非常重要的关键字,它允许程序员在编译时计算表达式的值,从而提高代码的性能和可预测性。随着C++标准的发展,constexpr在C++11和C++14之间经历了一些显著的改变。本文将详细探讨这些变化,并分析它们对C++编程实践的影响。一、C++11中的constexpr在C++11中,constexpr关键字的引入为编译时计算带来了革命性的变化。它允许程序员定义在编译时就能确定其值的变量和函数。这对于优化性能、减少运行时开销以及提高代码的可读性和可维护性都非常重要。在C++11中,constexpr函数的要求非常严格:函数体必须非常简单,通常只能包含一

c++ - gsl_vector 有 count_if 函数吗? C/C++

我正在使用gnu科学图书馆(GSL)。假设我有一个像这样的gsl_vector:70-658010-2这是一个包含正数、负数和零作为元素的vector。我想统计这个gsl_vector中非零元素或零元素的个数。我知道C++Vector有一个名为count_if的函数。但是我搜索了gsl_vector.h和gsl_blas.h,没有能与之匹敌的功能。我可以通过gsl_vector_get()评估它们来遍历所有元素,然后问if问题。intcounter=0;for(inti=0;i但是我想了将近一天,GSL中是否已经有这样一个效率更高的函数。或者gsl_array有一个count_if函数

c++ - 在 C++ 中同一类的另一个方法中使用 constexpr 方法

不出所料,我可以毫无问题地编译下面的示例//first_sample.cppstructsample_struct{constexprintsample_method(){return5;}};intmain(){sample_structsample_object;constexprintsample_variable=sample_object.sample_method();return0;}但由于以下原因我无法编译以下示例'this'isnotaconstantexpression//second_sample.cppstructsample_struct{constexpri

c++ - 将本地结构传递给 count_if

我正在尝试将匿名结构传递给std::count_if,但编译失败。当我尝试编译时(使用g++4.5.3,不使用c++03或c++11扩展),我在fail()方法中遇到错误,但是pass()方法没有那个错误。Infunction‘voidfail()’:Test.cpp:34:24:error:nomatchingfunctionforcallto‘count_if(std::map::iterator,std::map::iterator,fail()::&)’如果我将结构设为命名结构,我会遇到类似的错误。我不明白为什么在函数外部和内部声明它应该有所不同。我错过了什么?#include

c++ - remove_if 的一元谓词可以有副作用吗?

如果那些没有修改容器?例如,我想输出我从vector中删除的所有整数(我不想使用多次传递:例如:partition+output+erase)。撇开设计恐怖不谈,这是合法的:v.erase(remove_if(v.begin(),v.end(),[](constinti)->bool{if(i%2==0){coutAFAIK标准保证在每个元素上只应用一次谓词,所以我很好,因为我不关心顺序...... 最佳答案 标准确实保证了这一点,所以你没问题。不过,除了调试之外,我仍然认为它是糟糕的风格。

c++ - 不接受静态成员函数作为 constexpr 参数

以下代码被clang接受并被gcc拒绝。我想知道这是一个错误还是我遗漏了什么:#includetemplatestaticconstexprTApply(Tin,Tfun(T)){returnfun(in);}templatestructTriangle{usingAr=std::array;staticconstexprArfoo(Arline){returnline;}staticconstexprArresults=Apply({{1}},foo);//foo({1});isok};templateconstexprstd::arrayTriangle::results;intm

c++ - 非成员函数 begin()/cbegin() 及其constexpr-ness

C++11引入了没有constexpr-说明符的std::begin()非成员函数,然后C++14更新为constexpr-std::begin()用于数组类型(T(&)[N])并附加constexpr-std::cbegin()用于通用容器类型(constC&).引自http://en.cppreference.com/w/cpp/iterator/begintemplateconstexprT*begin(T(&array)[N]);//(sinceC++14)templateconstexprautocbegin(constC&c)->decltype(std::begin(c)