开发者

Default templated parameters in function templates

开发者 https://www.devze.com 2023-02-06 02:32 出处:网络
I was answering this question.And I realized that I ran my mouth off when I didn\'t know what I was talking about.

I was answering this question. And I realized that I ran my mouth off when I didn't know what I was talking about.

So my question is this. Is it possible to merge these functions in to one? (don't worry that this is an exact duplicate of a function that already exists, I'm just using it as an example)

template <class iterType1, class iterType2, class boolPred>
bool equal(iterType1 begin, iterType1 end, it开发者_运维技巧erType2 e, boolPred pred){

    while(begin != end){
        if(!pred(*begin, *e))
            return false;
        ++begin;
        ++e;
    }
    return true;
}

template <class iterType1, class iterType2>
bool equal(iterType1 begin, iterType1 end, iterType2 e){
    return equal(begin, end, e, std::equal_to<decltype(*begin)>());
}

Furthermore, is re-using the code from the first in the second even possible without using C++0x features(decltype).


Is it possible to merge these functions in to one?

Sadly, no. You can't have a default template argument for a function template parameter and default function arguments cannot be used to deduce template arguments.

Is re-using the code from the first in the second even possible without using C++0x features?

Yes: you can use std::iterator_traits<T>::value_type.

0

精彩评论

暂无评论...
验证码 换一张
取 消