简体   繁体   English

G ++,clang ++和std :: function

[英]G++, clang++ and std::function

I was just playing around with the new std::function from C++11, and I wrote an example that compiles with clang++ 3.2 and the Intel C++ compiler 13.1 but not with g++ 4.8. 我刚刚在C ++ 11中使用了新的std :: function,我编写了一个用clang ++ 3.2和Intel C ++编译器13.1编译但不用g ++ 4.8编译的例子。 Before I report this as a bug, I thought I'd check that I'm not doing something really stupid and that this should actually compile. 在我将此报告为错误之前,我想我会检查我是不是做了一些非常愚蠢的事情,而且这应该实际编译。 So, is the following code valid c++11? 那么,以下代码是否有效c ++ 11?

template <typename C>
void map(C& c, std::function<typename C::value_type(typename C::value_type)> f)
{
    for(auto& x : c) {
        x = f(x);
    }
}

int main()
{
    std::vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    map(v, [](int x) { return x+2; });

    for(auto x : v) {
        std::cout << x << std::endl;
    }
}

I realise that this code isn't very useful but it struck me as odd that clang and Intel C++ compiled it and gcc didn't. 我意识到这段代码并不是很有用,但令我感到奇怪的是clang和Intel C ++编译它并且gcc没有。

EDIT: gcc will also not compile the same code when passing map a functor or function pointer: 编辑:当传递map函子或函数指针时,gcc也不会编译相同的代码:

struct {
    int operator() (int a) {
        return a+2;
    }
} add2s;
map(v, add2s);

int add2 (int a) {
    return a+2;
}
map(v,add2);

clang and icpc also compile both of these. clang和icpc也编译这两个。

This is a G++ bug, it can be reduced to the following example which doesn't use std::function (or anything from the standard library): 这是一个G ++错误,它可以简化为以下示例,该示例不使用std::function (或标准库中的任何内容):

template<typename T>
struct function
{
    function(int)
    { }
};

struct V {
  typedef int value_type;
};

template <typename C>
void map(C&, function<typename C::value_type>)
{
}

int main()
{
  V v;
  map(v, 1);
}

I've reported it to bugzilla as PR 56874 . 我已经将它作为PR 56874报告给bugzilla。 The problem is not related to lambdas, but to a type in a non-deduced context incorrectly causing argument deduction to fail. 该问题与lambdas无关,而与非推导上下文中的类型无关,导致参数推断失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM