[英]Template argument deduction/substitution failed with std::set
我浏览了很多出现相同错误的帖子,但找不到适用于我的问题的帖子,如果这是重复的,我深表歉意。
无论如何,我的任务是制作一个名为 set_helper 的 class ,这使得 std::sets 更易于使用。 set_helper 将一个集合作为其构造函数参数,为了帮助进行模板类型推导,我还必须制作一个名为 make_set_helper 的 function,它应该使类型推导更容易。 我不完全理解这意味着什么,所以这就是我尝试过的。
带有main.cpp
和sethelp.h
的 MRE
main.cpp
#include<set>
#include "sethelp.h"
int main()
{
std::set<int, std::greater<int> > sg;
sg.insert( 0 );
sg.insert( 1 );
make_set_helper( sg );
return 0;
}
sethelp.h
#pragma once
template<class T>
class set_helper
{
private:
std::set<T>* s;
public:
set_helper(std::set<T>& s) { this->s = &s; }
bool operator==(T other) { return this == other; }
std::set<T>* get_set() {return this->s;}
};
template<class T>
set_helper<T> make_set_helper(std::set<T>& s)
{
return set_helper(s);
}
这是错误消息
error: no matching function for call to ‘make_set_helper(std::set<int, std::greater<int> >&)’
108 | make_set_helper( sg ) += sih;
note: candidate: ‘template<class T> set_helper<T> make_set_helper(std::set<T>&)’
79 | set_helper<T> make_set_helper(std::set<T>& s)
note: template argument deduction/substitution failed:
note: mismatched types ‘std::less<_Key>’ and ‘std::greater<int>’
为什么类型不匹配,我应该怎么做才能解决它?
std::set<T>
和std::set<T, std::greater<int>>
是完全不同的类型。 这是一个更通用的版本:
template<
class Key,
class Compare = std::less<Key>,
class Allocator = std::allocator<Key>
>
auto make_set_helper(std::set<Key, Compare, Allocator>& s)
{
return set_helper(s);
}
class 本身也应该具有所有这些模板参数。 或者:
template<class ...P>
auto make_set_helper(std::set<P...>& s)
{
return set_helper(s);
}
std::set
采用三个模板参数。 当您只指定第一个时,第二个(比较器)和第三个(分配器)是默认值。 所以std::set<T>
是std::set<T,std::less<T>,std::allocator<T>>
的缩写。 如果要接受具有不同比较器的集合,请使用两个模板参数:
#include <set>
template <typename T,typename Compare>
void foo(std::set<T,Compare>& x){}
template <typename T>
void bar(std::set<T>& x){}
int main(){
std::set<int> a;
std::set<int,std::greater<int>> b;
foo(a);
foo(b);
bar(a); // ok
//bar(b); // error
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.