简体   繁体   English

使用模板元编程C ++比较2个值

[英]Comparing 2 values using template metaprogramming C++

I want to have a default function as a "Predicate" template in case the user doesn't provide one. 我希望有一个默认功能作为“谓词”模板,以防用户不提供。 So far I've been doing something like: 到目前为止,我一直在做类似的事情:

template<typename T>
struct simple_compare{
    bool operator()(T const& a, T const& b){
        return a > b;
    }
};

template<typename T, typename Predicate=simple_compare<T> >
bool compare(T a, T b, Predicate pred) {
    return pred(a, b);
}

Can this be done using template metaprogramming in C++ instead of having a struct with an overloaded () operator? 可以使用C ++中的模板元编程来完成此操作,而不要使用带有重载()运算符的结构吗?

You do not need fancy template metaprogramming things. 您不需要花哨的模板元编程内容。 Simply create two versions of the template function. 只需创建两个版本的模板函数。 The one without the custom predicate will simply execute the default one. 没有自定义谓词的将只执行默认谓词。

Something as the following should works: 以下内容应能起作用:

auto default_pred = [](const auto a, const auto b) {return a > b;};
auto custom_pred  = [](const auto a, const auto b) {return a < b;};


template<typename T, typename Fn >
bool compare2(T a, T b, Fn pred) {
    return pred(a, b);
}

template<typename T >
bool compare2(T a, T b) {
    return default_pred (a, b);
}

int main(){
    cout<<compare2(2, 4)<<endl;
    cout<<compare2(10.2d, 4.5d, custom_pred)<<endl;
   return 0;
}

There is no need for template metaprogramming here. 这里不需要模板元编程。 You can simply use overloading like shown in the answer by Davide Spataro to provide a version that doesn't take a predicate and just calls the full version with a default predicate. 您可以简单地使用重载(如Davide Spataro的答案所示)来提供不带谓词的版本,而仅使用默认谓词调用完整版本。 Or you can just use a default argument for your predicate: 或者,您可以仅对谓词使用默认参数:

template <typename T, typename Predicate = simple_compare<T>>
bool compare(T a, T b, Predicate pred = {}) {
    return pred(a, b);
}

If you just want a generic functor that invokes the > operator, then you could also just make the operator () a template instead of the functor type itself and let the exact types to compare be deduced from the call: 如果您只想使用泛型仿函数来调用>运算符,则也可以仅使operator ()成为模板而不是仿函数类型本身,并从调用中推导要比较的确切类型:

struct simple_compare {
    template <typename A, typename B>
    bool operator()(A const& a, B const& b) const {
        return a > b;
    }
};

template <typename T, typename Predicate = simple_compare>
bool compare(T a, T b, Predicate pred = {}) {
    return pred(a, b);
}

Also, the standard library already provides standard functors for invoking all sorts of operators. 而且,标准库已经提供了用于调用各种运算符的标准函子。 So instead of rolling your own, you could just use std::greater<T> or std::greater<void> in your example. 因此,在示例中,您可以只使用std::greater<T>std::greater<void>来代替自己滚动。 Furthermore, I assume there is no real need to require your arguments to be copyable and of the same type: 此外,我认为并没有必要要求您的参数是可复制的并且具有相同类型:

template <typename A, typename B, typename Predicate = std::greater<void>>
bool compare(A const& a, B const& b, Predicate pred = {}) {
    return pred(a, b);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM