C++ 不能';t从函数参数'推导模板参数;s默认参数
我试着做一个函数,在满足给定条件的范围内找到最小元素:C++ 不能';t从函数参数'推导模板参数;s默认参数,c++,templates,c++11,C++,Templates,C++11,我试着做一个函数,在满足给定条件的范围内找到最小元素: #include <functional> #include <iostream> #include <vector> template <typename It, typename Pred, typename Comp> It minElementWhere( It begin, It end, Pred pred = Pred(), // Use le
#include <functional>
#include <iostream>
#include <vector>
template <typename It, typename Pred, typename Comp>
It minElementWhere(
It begin,
It end,
Pred pred = Pred(),
// Use less-than as the default comparator.
Comp comp = std::less<decltype(*std::declval<It>())>()
) {
It minElement = end;
for (It it = begin; it != end; ++it) {
if (!pred(*it)) {
continue;
}
if (comp(*it, *minElement)) {
minElement = it;
}
}
return minElement;
}
int main() {
std::vector<double> foo;
foo.push_back(6);
foo.push_back(10);
foo.push_back(-3);
foo.push_back(7);
std::cout << *minElementWhere(
foo.begin(),
foo.end(),
[](double val) {
return val >= 0;
}
) << std::endl;
}
#包括
#包括
#包括
模板
它在哪里(
开始的时候,
结束了,,
Pred Pred=Pred(),
//使用小于作为默认比较器。
Comp Comp=std::less()
) {
它意味着结束;
for(It=begin;It!=end;++It){
如果(!pred(*it)){
继续;
}
如果(复合(*it,*minElement)){
minElement=it;
}
}
返回元素;
}
int main(){
std::矢量foo;
foo.推回(6);
foo.推回(10);
foo.向后推_(-3);
foo.推回(7);
std::cout=0;
}
)这对我很有用:
template <typename It, typename Pred,
typename Comp = std::less<decltype(*std::declval<It>())>>
It minElementWhere(
It begin,
It end,
Pred pred = Pred(),
Comp comp = Comp()
) {
It minElement = end;
for (It it = begin; it != end; ++it) {
if (!pred(*it)) {
continue;
}
if (comp(*it, *minElement)) {
minElement = it;
}
}
return minElement;
}
模板
它在哪里(
开始的时候,
结束了,,
Pred Pred=Pred(),
Comp Comp=Comp()
) {
它意味着结束;
for(It=begin;It!=end;++It){
如果(!pred(*it)){
继续;
}
如果(复合(*it,*minElement)){
minElement=it;
}
}
返回元素;
}
您可能需要以下内容:
#include <iterator>
struct always_true{
template<typename T>
bool operator()(T&& val) const{
return true;
}
};
template <
typename It,
typename Pred = always_true,
typename Comp = std::less<typename std::iterator_traits<It>::value_type >
>
It minElementWhere(
It begin,
It end,
Pred pred = Pred(),
Comp comp = Comp()
){
It minElement = end;
for (It it = begin; it != end; ++it) {
if (pred(*it) && (minElement == end || comp(*it, *minElement))){
minElement = it;
}
}
return minElement;
}
#包括
结构总是正确的{
模板
布尔运算符()(T&&val)常量{
返回true;
}
};
模板<
键入名称,
typename Pred=始终为真,
typename Comp=std::less
>
它在哪里(
开始的时候,
结束了,,
Pred Pred=Pred(),
Comp Comp=Comp()
){
它意味着结束;
for(It=begin;It!=end;++It){
if(pred(*it)和&(minElement==end | | comp(*it,*minElement))){
minElement=it;
}
}
返回元素;
}
您希望模板参数Comp
是从为相应函数参数提供的默认参数推导出来的。但是,这被明确列为非推断上下文,这意味着该模板参数的模板参数推断将失败(除非可以从其他地方推断)
根据§14.8.2.5/5【临时扣减类型】
非推断上下文为:
— ...
-模板的参数类型中使用的模板参数
具有在中使用的默认参数的函数参数
正在为其执行参数推断的调用
要使模板参数推断成功,请为模板参数而不是函数参数提供默认参数
template <typename It,
typename Pred,
typename Comp = std::less<decltype(*std::declval<It>())>>
It minElementWhere(
It begin,
It end,
Pred pred = Pred(),
Comp comp = Comp()
) {
...
}
模板
它在哪里(
开始的时候,
结束了,,
Pred Pred=Pred(),
Comp Comp=Comp()
) {
...
}
无法从默认参数推断模板参数。[OT]:您的实现是UB,正如您所做的那样comp(*It,*end)
。
template <typename It,
typename Pred,
typename Comp = std::less<decltype(*std::declval<It>())>>
It minElementWhere(
It begin,
It end,
Pred pred = Pred(),
Comp comp = Comp()
) {
...
}