为什么 std::less<Eigen::VectorXd> 无法编译?

7
我为 Eigen::VectorXd 实现了比较运算符operator<。有时,我需要将比较函数传递给我的另一个函数。我厌倦了将 operator< 包装成 [](const VectorXd& v1, const VectorXd& v2)->bool{return v1 < v2}。因此,我认为 std::less 类会很有用,因为据我所知,只要定义了 operator<,它就可以生成 lambda 函数。
然而,我发现对于我来说,std::less<VectorXd> 不起作用。例如,下面的代码可以正常工作:
#include "Eigen/Dense"
#include <iostream>
#include <functional>

using namespace std;
using namespace Eigen;

struct T
{
    int x;
};

bool operator<(const T& t1, const T& t2)
{
    return t1.x < t2.x;
}

bool operator<(const VectorXd& v1, const VectorXd& v2)
{
    return (v1.array() <= v2.array()).all() and (v1 != v2);
}
int main()
{
    T t1, t2;
    t1.x = 3;
    t2.x = 2;

    auto ft = std::less<T>();
    cout << ft(t1, t2) << endl;

    return EXIT_SUCCESS;
}

然而,如果我像这样使用std::less<VectorXd>
#include "Eigen/Dense"
#include <iostream>
#include <functional>

using namespace std;
using namespace Eigen;

struct T
{
    int x;
};

bool operator<(const T& t1, const T& t2)
{
    return t1.x < t2.x;
}

bool operator<(const VectorXd& v1, const VectorXd& v2)
{
    return (v1.array() <= v2.array()).all() and (v1 != v2);
}
int main()
{
    T t1, t2;
    t1.x = 3;
    t2.x = 2;

    auto ft = std::less<T>();
    cout << ft(t1, t2) << endl;

    VectorXd v1(3);
    VectorXd v2(3);
    v1 << 1, 2, 3;
    v2 << 2, 3, 4;
    auto fv = std::less<VectorXd>();
    cout << fv(v1, v2) << endl;

    return EXIT_SUCCESS;
}

代码无法编译,我收到以下错误信息:
In file included from /usr/include/c++/5/string:48:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_function.h: In instantiation of ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Eigen::Matrix<double, -1, 1>]’:
test.cpp:36:26:   required from here
/usr/include/c++/5/bits/stl_function.h:387:20: error: no match foroperator<’ (operand types are ‘const Eigen::Matrix<double, -1, 1>’ andconst Eigen::Matrix<double, -1, 1>’)
       { return __x < __y; }
                    ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:220:5: note: candidate: template<class _T1, class _T2> constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
     operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
     ^
/usr/include/c++/5/bits/stl_pair.h:220:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/string:48:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_function.h:387:20: note:   ‘const Eigen::Matrix<double, -1, 1>’ is not derived from ‘const std::pair<_T1, _T2>’
       { return __x < __y; }
                    ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_iterator.h:298:5: note: candidate: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
     operator<(const reverse_iterator<_Iterator>& __x,
     ^
/usr/include/c++/5/bits/stl_iterator.h:298:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/string:48:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_function.h:387:20: note:   ‘const Eigen::Matrix<double, -1, 1>’ is not derived from ‘const std::reverse_iterator<_Iterator>’
       { return __x < __y; }
                    ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_iterator.h:348:5: note: candidate: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
     operator<(const reverse_iterator<_IteratorL>& __x,
     ^
/usr/include/c++/5/bits/stl_iterator.h:348:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/string:48:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_function.h:387:20: note:   ‘const Eigen::Matrix<double, -1, 1>’ is not derived from ‘const std::reverse_iterator<_Iterator>’
       { return __x < __y; }
                    ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_iterator.h:1089:5: note: candidate: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::move_iterator<_Iterator>&, const std::move_iterator<_IteratorR>&)
     operator<(const move_iterator<_IteratorL>& __x,
     ^
/usr/include/c++/5/bits/stl_iterator.h:1089:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/string:48:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_function.h:387:20: note:   ‘const Eigen::Matrix<double, -1, 1>’ is not derived from ‘const std::move_iterator<_Iterator>’
       { return __x < __y; }
                    ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_iterator.h:1095:5: note: candidate: template<class _Iterator> bool std::operator<(const std::move_iterator<_Iterator>&, const std::move_iterator<_Iterator>&)
     operator<(const move_iterator<_Iterator>& __x,
     ^
/usr/include/c++/5/bits/stl_iterator.h:1095:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/string:48:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_function.h:387:20: note:   ‘const Eigen::Matrix<double, -1, 1>’ is not derived from ‘const std::move_iterator<_Iterator>’
       { return __x < __y; }
                    ^
In file included from /usr/include/c++/5/string:52:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/basic_string.h:4987:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/include/c++/5/bits/basic_string.h:4987:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/string:48:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_function.h:387:20: note:   ‘const Eigen::Matrix<double, -1, 1>’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
       { return __x < __y; }
                    ^
In file included from /usr/include/c++/5/string:52:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/basic_string.h:4999:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/include/c++/5/bits/basic_string.h:4999:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/string:48:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_function.h:387:20: note:   ‘const Eigen::Matrix<double, -1, 1>’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
       { return __x < __y; }
                    ^
In file included from /usr/include/c++/5/string:52:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/basic_string.h:5011:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
     operator<(const _CharT* __lhs,
     ^
/usr/include/c++/5/bits/basic_string.h:5011:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/string:48:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_function.h:387:20: note:   mismatched types ‘const _CharT*’ and ‘Eigen::Matrix<double, -1, 1>’
       { return __x < __y; }
                    ^
In file included from /usr/include/c++/5/bits/ios_base.h:46:0,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/system_error:200:3: note: candidate: bool std::operator<(const std::error_code&, const std::error_code&)
   operator<(const error_code& __lhs, const error_code& __rhs) noexcept
   ^
/usr/include/c++/5/system_error:200:3: note:   no known conversion for argument 1 from ‘const Eigen::Matrix<double, -1, 1>’ toconst std::error_code&’
/usr/include/c++/5/system_error:274:3: note: candidate: bool std::operator<(const std::error_condition&, const std::error_condition&)
   operator<(const error_condition& __lhs,
   ^
/usr/include/c++/5/system_error:274:3: note:   no known conversion for argument 1 from ‘const Eigen::Matrix<double, -1, 1>’ toconst std::error_condition&’
In file included from /usr/include/c++/5/tuple:39:0,
                 from /usr/include/c++/5/functional:55,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:262,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/array:250:5: note: candidate: template<class _Tp, long unsigned int _Nm> bool std::operator<(const std::array<_Tp, _Nm>&, const std::array<_Tp, _Nm>&)
     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
     ^
/usr/include/c++/5/array:250:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/string:48:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_function.h:387:20: note:   ‘const Eigen::Matrix<double, -1, 1>’ is not derived from ‘const std::array<_Tp, _Nm>’
       { return __x < __y; }
                    ^
In file included from /usr/include/c++/5/functional:55:0,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:262,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/tuple:928:5: note: candidate: template<class ... _TElements, class ... _UElements> constexpr bool std::operator<(const std::tuple<_Elements ...>&, const std::tuple<_Elements ...>&)
     operator<(const tuple<_TElements...>& __t,
     ^
/usr/include/c++/5/tuple:928:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/string:48:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_function.h:387:20: note:   ‘const Eigen::Matrix<double, -1, 1>’ is not derived from ‘const std::tuple<_Elements ...>’
       { return __x < __y; }
                    ^
In file included from /usr/include/c++/5/vector:64:0,
                 from /usr/include/c++/5/bits/random.h:34,
                 from /usr/include/c++/5/random:49,
                 from /usr/include/c++/5/bits/stl_algo.h:66,
                 from /usr/include/c++/5/algorithm:62,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:269,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_vector.h:1528:5: note: candidate: template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
     operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
     ^
/usr/include/c++/5/bits/stl_vector.h:1528:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/string:48:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/sstream:38,
                 from /usr/include/c++/5/complex:45,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Core:80,
                 from /home/alaya/.softwares/linear_algebra/eigen3.3/include/eigen3/Eigen/Dense:1,
                 from test.cpp:1:
/usr/include/c++/5/bits/stl_function.h:387:20: note:   ‘const Eigen::Matrix<double, -1, 1>’ is not derived from ‘const std::vector<_Tp, _Alloc>’
       { return __x < __y; }
                    ^
make: *** [try] Error 1

我正在使用 g++ 5.4.0 和 Eigen 3.3


请包含一个完整可运行的代码。#include "Eigen/Dense"在哪里? - Souradeep Nanda
2
@SouradeepNanda:这是Eigen库的一部分,这个问题就是关于它的。我认为在OP的示例中包含它是可以的,因为这个问题特别涉及到它(并且他们试图通过使用自己的struct T进一步减少问题,但未能复制问题)。 - Cornstalks
抱歉。 - Souradeep Nanda
(v1.array() <= v2.array()).all() and (v1 != v2) 只定义了半序,即你会有向量满足 !(v1<v2) && !(v2<v1) && (v1!=v2),这不是大多数 std-algorithms/containers 所期望的。如果你想要一个完全排序的向量,你可以实现一个词典序排序。 - chtz
@chtz 我知道我的 operator< 实现只是部分排序,实际上我正在实现一个多目标优化算法,其中“<”通常用于表示帕累托支配。比较函数不会传递给 std 算法。 - Alaya
1个回答

9

事情是这样的,你的T不能可靠地重新创建情况。你省略了命名空间,这非常重要。一个更接近于Eigen的例子如下:

namespace Foo {
  struct T
  {
    int x;
  };
}

bool operator<(const Foo::T& t1, const Foo::T& t2)
{
    return t1.x < t2.x;
}

并且它会产生完全相同的错误。这是因为模板只考虑在模板定义点(而不是实例化)找到的运算符重载,或者通过操作数类型的参数依赖查找找到的运算符重载。

而要使参数依赖查找起作用,运算符和类型必须在相同的命名空间中定义,而你的命名空间显然不是这样的。

所以发生的情况是这样的,你包含了functional,这在你的翻译单元中定义了模板std::less。那个时候,看不到Eigen::VectorXdoperator<。因此,在稍后定义时,模板定义将不予考虑它。

当你实例化模板时,它会尝试从已知的重载中寻找适当的重载,然后再通过ADL查找。由于你的operator<不在Eigen命名空间内,因此它也没有被ADL找到。

所以长话短说,为库类型重载运算符是不切实际的。你应该做的是定义一个自定义比较器类型:

struct VectorXdCompare {
  bool operator()(const VectorXd& v1, const VectorXd& v2) {
     return (v1.array() <= v2.array()).all() and (v1 != v2);
  }
};

当标准库需要时,将VectorXdCompare作为类型传递。


另一种选择是重新打开 namespace Eigen 来放置 operator<,但是 VectorXdCompare 更可取,当您无法控制使用比较器时,可以使用 std::less 作为替代方案。 - Caleth
似乎在命名空间中使用 T 也能正常工作。https://wandbox.org/permlink/TMNXF26aL1QUzCNl 编辑:忘记需要实际调用它了。https://wandbox.org/permlink/hf3jV8rWubgU0ov8 +1 - Baum mit Augen
那是一个非常棒的答案! - vsoftco
@vsoftco - 谢谢!虽然我认为如果我没有抵制添加“在Foo::T中开枪”的双关语的冲动会更好 :) - StoryTeller - Unslander Monica

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接