模板参数推导/替换失败, std::set

问题描述

我浏览了很多帖子都遇到了同样的错误,但找不到适用于我的问题的帖子,如果这是重复的,我深表歉意。

无论如何,我的任务是创建一个名为 set_helper 的类,它使 std::sets 更易于使用。 set_helper 将集合作为其构造函数参数,为了帮助进行模板类型推导,我还必须创建一个名为 make_set_helper 的函数,该函数应该使类型推导更容易。我不完全明白这意味着什么,所以这是我尝试过的。

带有 main.cppsethelp.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);
}

类本身也应该具有所有这些模板参数。或者:

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
}