传递lambda作为参数时,参数推断失败

时间:2015-12-10 08:45:29

标签: c++ function lambda

我有自己的功能;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#404040"
    android:orientation="vertical"
    android:weightSum="3">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_above="@+id/tv1"
            android:autoText="false"
            android:background="#000000"
            android:elegantTextHeight="false"
            android:inputType="number"
            android:maxLength="11"
            android:singleLine="true"
            android:text="12345678901"
            android:textAlignment="center"
            android:textColor="#a0a0a0"
            android:textSize="40dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="tv1"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#55ff1c" />
    </LinearLayout>

    <CheckBox
        android:id="@+id/cbx1"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:singleLine="true"
        android:text="bla bla bla bla bla"
        android:textSize="18dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="2.8"
        android:layout_margin="10dp"
        android:layout_height="0dp"
        android:orientation="horizontal">

        <ListView
            android:id="@+id/listView"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp">
                <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="RadioButton1"/>
                <RadioButton
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="RadioButton2"/>
            </RadioGroup>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginTop="20dp">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:text="Text View" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:text="Text View" />
            </LinearLayout>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:padding="5dp"
                android:text="button" />
        </LinearLayout>
    </LinearLayout>
    <TextView
        android:layout_gravity="bottom"
        android:layout_width="match_parent"
        android:layout_weight="0.2"
        android:layout_margin="10dp"
        android:layout_height="wrap_content"
        android:text="Bottomtextview"/>
</LinearLayout>

当我调用此函数时:

template < typename T > 
inline 
vector<T> listIntersection (  vector<T>& v1, vector<T>& v2, function<bool(T, T)> comprarer )

论据扣除失败。 但是,如果我使用v_intersection = listIntersection( v1, v2, []( A left, A right ) { return left.getNumber() < right.getNumber(); }); 如下;

<A>

程序编译。为什么模板参数推导在第一种情况下失败?

没有必要,但下面可以看到完整的例子;

v_intersection = listIntersection<A>( v1, v2, []( A left, A right )
{
    return left.getNumber() < right.getNumber();
});

1 个答案:

答案 0 :(得分:2)

闭包对象不是std::function,它有一些未命名的类型。编译器无法在一步中隐式转换为具有未知模板参数的std::function

如果不想显式传递模板参数,可以只将函数参数设为模板参数类型,而不是提交到std::function

template < typename T, typename Fn > 
inline 
vector<T> listIntersection (  vector<T>& v1, vector<T>& v2, Fn comprarer )
{
    //...
}