小编Tom*_*mis的帖子

为什么模板参数推导在重载函数中失败?

我有一个模板函数,应该使用函数指针和参数,然后使用给定的参数调用函数指针(让我们称之为Invoke)。但是,当我使用重载函数作为参数调用模板函数时,模板推导失败。

我使用过enable_if,以便只有一个重载有效,但这没有帮助。

#include <string>
#include <type_traits>

void foo(int, int){}
void foo(std::string, std::string) {}

template <bool Val1, bool Val2, bool ...Rest>
struct And
{
    enum {value = And<Val1 && Val2, Rest...>::value};
};
template <bool Val1, bool Val2>
struct And<Val1, Val2>
{
    enum {value = Val1 && Val2};
};

template <typename ...Params, typename ...Args, typename = typename std::enable_if<
    And<std::is_convertible<Args, Params>::value...>::value
>::type>
void Invoke(void (*fn)(Params...), Args ...args){}

int main() {
    Invoke(&foo, "a", "b");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

尝试使用ideone

mismatched argument …

c++ language-lawyer template-argument-deduction

4
推荐指数
2
解决办法
69
查看次数

C#List.Sort不排序

我有一个List<T>我想要排序,所以我用过List<T>.Sort(Comparison<T>).我的代码没有按预期工作,经过一些调试后我发现虽然包含在内的项目的顺序确实发生了变化,但它并没有被排序.
代码在这里:

System.Comparison<Intersection> comp=(Intersection one, Intersection other)=>{//Sort sorts from lowest to highest
    if(one.index>other.index){
        return 1;
    }
    else if(one.index<other.index){
        return -1;
    }
    else if((one.point-one.node.position).sqrMagnitude>(other.point-other.node.position).sqrMagnitude){
        return 1;
    }
    else{
        return -1;
    }
};
intersections.Sort(comp);
Run Code Online (Sandbox Code Playgroud)

麻烦的是,在排序之后,可以按顺序找到项目,使得第三项具有索引7而第四项具有6.我认为比较lambda可能有问题,但我添加了使用相同功能的代码比较顺序项,但它表现正常,有时返回1,所以问题显然在其他地方.
后来的比较:

for(int he=1; he<intersections.Count; he++){
    Debug.Log(comp(intersections[he-1], intersections[he]));
}
Run Code Online (Sandbox Code Playgroud)

是否有我缺少的东西或我的List<T>.Sort实现有缺陷我应该制作自己的排序方法?

结构如下所示:

class Intersection{
    public PolyNode node;
    public int index;
    public Polygon poly;
    public Intersection sister;
    public bool is_out;
    public sbyte wallnum;
    public Vector2 point;
    public int list_index;
}
Run Code Online (Sandbox Code Playgroud)

c# sorting

1
推荐指数
2
解决办法
1702
查看次数