Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在函数参数中使用模板参数不适用于gcc4.8_C++_Templates_Gcc_C++11_Lambda - Fatal编程技术网

C++ 在函数参数中使用模板参数不适用于gcc4.8

C++ 在函数参数中使用模板参数不适用于gcc4.8,c++,templates,gcc,c++11,lambda,C++,Templates,Gcc,C++11,Lambda,我有这样一个函数: template<typename Iterator> void sort2(Iterator it, std::function<bool(typename std::remove_pointer< typename Iterator::iterator_type>::type,

我有这样一个函数:

template<typename Iterator>
void sort2(Iterator it,
           std::function<bool(typename std::remove_pointer<
                              typename  Iterator::iterator_type>::type,
                                          int)> func)
{
}

int main()
{
    std::vector<int> a;
    sort2(a.begin(),[](int,int){return false;});
}
'main()::__lambda0' is not derived from 'std::function<bool(typename std::remove_pointer<typename Iterator::iterator_type>::type, int)>'
 sort2(a.begin(),[](int,int){return false;});
                                           ^
模板
void sort2(迭代器它,
std::function::type,
int)>func)
{
}
int main()
{
std::载体a;
sort2(a.begin(),[](int,int){return false;});
}
'main()::_lambda0'不是从'std::function'派生的
sort2(a.begin(),[](int,int){return false;});
^
当我将其更改为:

template<typename Iterator>
void sort2(Iterator it,
           std::function<bool(typename std::remove_pointer<
                              typename  vector<int>::iterator/*I change this*/::iterator_type>::type,
                                          int)> func)
{
}
模板
void sort2(迭代器它,
std::function::type,
int)>func)
{
}
它编译得很好

第一个函数有什么问题


这似乎是正确的。。。为什么它会给出编译错误?

迭代器没有名为
迭代器\u type
的成员。您需要
Iterator::value\u type
():


这是非常丑陋的-SFINAE的约束总是-。

迭代器::迭代器类型将是标记类型(在本例中,std::random_访问)。您可能只需要
Iterator::reference
,尽管我会指出,我只需要模板化第二个参数,以便也可以使用函数指针和运算符结构。注意:传递单个迭代器而不是范围将不起作用(尽管这是一个示例)您的代码在gcc-4.9和clang-3上编译。4@MadScienceDreams
Iterator::Iterator\u type
不会是任何东西-您正在考虑
std::Iterator\u traits::Iterator\u category
。gcc仍然没有编译它。对我来说,
iterator\u type
看起来也很奇怪,但我找到了一些MSDN文档,其中谈到了它。@omid gcc编译了它。所以我想这里的问题是哪个编译器错了。
template<typename Iterator>
void sort2(Iterator it,
           std::function<bool(typename std::remove_pointer<
                              typename Iterator::value_type>::type,
                                          int)> func)
{
}
template<typename Iterator>
void sort2(Iterator it,
           std::function<bool(typename std::remove_pointer<
                              typename std::iterator_traits<Iterator>::value_type>::type,
                                          int)> func)
{
}
template <typename Iterator>
using ValueType = typename std::iterator_traits<Iterator>::value_type;

template<typename Iterator, typename Function>
auto sort2(Iterator first, Iterator last, Function func) ->
  typename std::enable_if<
    std::is_convertible<decltype(func(std::declval<ValueType<Iterator>>(),
                                      std::declval<ValueType<Iterator>>())),
                        bool>::value
  >::type
{
    std::sort(first, last, func);
}