C++ 未对基于范围的进行优化

C++ 未对基于范围的进行优化,c++,optimization,c++17,vectorization,clang++,C++,Optimization,C++17,Vectorization,Clang++,下面的函数f()。有什么想法吗?我不能让它工作。见: 另外,R如果根据其定义替换了基于范围的循环,则应该是正确的-但clang似乎使用了错误的end()位置。为什么? template< typename C, typename F > struct R : C::const_iterator { using it = typename C::const_iterator; R( C &pc, F && pf ) : it( pc.begin() ),

下面的函数
f()。有什么想法吗?我不能让它工作。见:

另外,
R
如果根据其定义替换了基于范围的循环,则应该是正确的-但clang似乎使用了错误的
end()
位置。为什么?

template< typename C, typename F >
struct R : C::const_iterator {
  using it = typename C::const_iterator;
  R( C &pc, F && pf ) : it( pc.begin() ), c( pc ), f( pf ) {}
  C &c; F f;
  inline bool operator!=( it ) {
    for( ; __builtin_expect( static_cast< it >( *this ) != c.end(), 1 ); it::operator++() )
      if( __builtin_expect( f( it::operator*() ) , 1 ) ) return true;
    return false;
  }
  inline decltype( auto ) begin() { return *this; }
  inline decltype( auto ) end() { return c.end(); }
};

template< typename C, typename F >
inline decltype( auto ) operator | ( C &c, F &&f )
 { return R< C, F >( c, f ); }

int f( std::vector< int > const& v ) {
  int k = 0;
  for( auto e : v | []( auto x ){ return x > 10; } )
    k += e;
  return k;
}

int g( std::vector< int > const& v ) {
  int k = 0;
  for( auto e : v )
    if( e > 10 ) k += e;
  return k;
}
模板
结构R:C::常量迭代器{
使用它=类型名C::常量迭代器;
R(C&pc,F&pf):it(pc.begin()),C(pc),F(pf){}
C&C;F;
内联布尔运算符!=(it){
对于(;u_内置_expect(static_cast(*this)!=c.end(),1);it::operator++())
if(uuu builtin_uexpect(f(it::operator*()),1))返回true;
返回false;
}
内联decltype(自动)begin(){return*this;}
内联decltype(auto)end(){return c.end();}
};
模板
内联decltype(自动)运算符|(C&C、F&F)
{返回R(C,F);}
intf(标准::向量const&v){
int k=0;
对于(自动e:v |[](自动x){返回x>10;})
k+=e;
返回k;
}
int g(标准::向量const&v){
int k=0;
用于(自动e:v)
如果(e>10)k+=e;
返回k;
}

我认为你应该将你的句柄从cppnoob改为cppfreak:)一般建议:不要把简单的字母都用在上面。您应该为类、方法和成员使用好的名称。