Function c++;11:创建函数的最简单方法总是返回true以用于函数参数

Function c++;11:创建函数的最简单方法总是返回true以用于函数参数,function,c++11,lambda,Function,C++11,Lambda,我想使用以下模板成员函数 template <typename Entity> class SomeCollection { // .... template <typename Measure, typename Filter> Entity maximalEntity(Measure&& measure, Filter&& requirement) { auto maxEntity =

我想使用以下模板成员函数

template <typename Entity>
class SomeCollection
{
    // ....

    template <typename Measure, typename Filter>
    Entity maximalEntity(Measure&& measure, Filter&& requirement)
    {
        auto maxEntity = Entity();
        auto maxValue = -std::numeric_limits<double>::infinity();

        for (auto ent /* some iteration method*/)
        {
            auto measurement = measure(ent);
            if (requirement(ent) && measurement > maxValue)
                std::tie(maxEntity, maxValue) = std::make_tuple { ent, measurement };
        }
        return maxEntity;
    }

    // ...
};
模板
类集合
{
// ....
模板
实体最大值(度量和度量、筛选和要求)
{
auto maxEntity=实体();
自动最大值=-std::numeric_limits::infinity();
对于(自动ent/*某些迭代方法*/)
{
自动测量=测量(ent);
if(要求(ent)和测量>最大值)
std::tie(maxEntity,maxValue)=std::make_tuple{ent,measurement};
}
返回最大实体;
}
// ...
};
从客户机代码调用此函数而不需要筛选要求(只需要最大元素)的最佳方法是什么

我能想到的最好办法是:

class Something;
double measure(Something&);
SomeCollection<Something> collection;

auto maximum = collection.maximalEntity(measure, [](const Something&) { return true; });
给某物分类;
双重测量(某物&);
收藏;
自动最大值=collection.maximalEntity(度量,[](const Something&){return true;});

但是我想这个lambda函数可以改进不?

不确定如何改进lambda,但是您可以定义一个通用lambda,给定任何输入都将始终返回true(这里也可以使用):

您可以将其用作:

auto maximum = collection.maximalEntity(measure, always_true);


C++11的等效实现如下所示:

struct always_true {
    template<typename... Args>
    bool operator()(Args&&...) const noexcept {
        return true;
    }
};

您可以创建一个lambda,返回true并将其设置为默认参数

auto true_filter = [](const Something& arg){ return true; };
//auto true_filter = [](auto&& arg){ return true; }; if you have c++14
...

template <typename Measure, typename Filter = decltype(true_filter)>
Entity maximalEntity(Measure&& measure, Filter requirement = true_filter)
{

...
auto maximum = collection.maximalEntity(measure);
auto-true_-filter=[](const-Something&arg){return-true;};
//auto-true_-filter=[](auto&&arg){return-true;};如果你有c++14
...
模板
实体最大值(度量值和度量值,过滤要求=真实过滤)
{
...
自动最大值=收集。最大值(度量);
注意,
Filter
已从
Filter&
更改为
Filter&

虽然有明确的说明可能是更好的设计。只是一个选项,让它“更短”

C++14:

template<class T>
auto always() {
  return [](auto&&...)->T{return {};};
};
模板
自动始终(){
return[](auto&&…->T{return{};};
};
或者在C++11中:

template<class T>
struct always {
  template<class...Args>
  T operator()(Args&&...)const{ return {}; }
};
模板
结构始终{
模板
T运算符()(Args&&…)const{return{};}
};
使用:

collection.maximalEntity(measure,always());
这样做的好处是,所涉及的lambda的真实性是在类型系统中编码的,这使得编译器更容易优化其行为

这还允许您执行
始终
始终()
等操作

在C++17中,我会:

template<auto x>
auto always() {
  return [](auto&&)->std::integral_constant<decltype(x), x>
  { return {}; };
}
模板
自动始终(){
返回[](自动&)->std::积分常数
{返回{};};
}

它允许
总是()
总是()
(也许
总是()
?).

多态lambda是在C++14中添加的,问题被标记为C++11。您无法让它使用右值引用的原因是您将
始终为true
定义为左值。如果将默认模板参数设为左值引用,则
过滤器和
将起作用,即
decltype(true\u过滤器)&
回答得很好。只是吹毛求疵(或者可能是风格问题):在C++11版本中,您可以使
struct always\u true\u t
然后
const always\u t always\u true
template<class T>
struct always {
  template<class...Args>
  T operator()(Args&&...)const{ return {}; }
};
collection.maximalEntity(measure, always<std::true_type>());
template<auto x>
auto always() {
  return [](auto&&)->std::integral_constant<decltype(x), x>
  { return {}; };
}