被叫对象类型'auto'不是函数或函数指针

时间:2015-02-07 19:30:23

标签: c++ lambda c++14 template-meta-programming

当我尝试编译使用lambda类型的变量模板的C ++ 14代码时,我发现Clang(3.4和3.5)出现了一个奇怪的错误。

Here's the C++14 code as I'd like to write it.

Here's a second version,删除了一些库_t_v内容以使编译器满意:

#include <type_traits>

template <class T>
class forward_if_wrapper {

    template <class U, class Enable = typename std::enable_if<std::is_lvalue_reference<U>::value>::type>
    static U forward(U&& u) {
        return u;
    }

    template <class U, class Enable = typename std::enable_if<!std::is_lvalue_reference<U>::value>::type>
    static U&& forward(U&& t) {
        return static_cast<U&&>(t);
    }
};
auto forward = [](auto&& t) { return forward_if_wrapper<decltype(t)>::forward(t); };
template <class T> auto forward_if = [](auto&& u) { return forward_if_wrapper<T>::forward(u); };

// --------

#include <stdio.h>
#include <vector>

template<class Elt>
void bar(Elt&& e) {
    printf("Called %s\n", __PRETTY_FUNCTION__);
}

template<class Container>
void foo(Container&& c) {
    for (auto&& elt : c) {
        bar(forward_if<Container>(elt));
    }
}

int main() {
    std::vector<int> v = {1,2};
    foo(v);
    foo(std::move(v));
}

我从Clang的第二个代码中看到的错误是:

test.cc:34:13: error: called object type 'auto' is not a function or function pointer
        bar(forward_if<Container>(elt));
            ^~~~~~~~~~~~~~~~~~~~~~~
test.cc:43:5: note: in instantiation of function template specialization 'foo<std::__1::vector<int, std::__1::allocator<int> > &>' requested here
    foo(v);
    ^

我无法访问任何支持C ++ 14通用lambdas的其他编译器,所以我已经测试过Clang。

这是我的错误还是Clang的错误?

我倾向于认为它是Clang模板实例化规则中的一个错误:Clang似乎将auto视为类型世界中的一等公民,而不是作为执行类型扣除的指标。

test.cc:34:14: error: invalid argument type 'auto' to unary expression
        bar((+forward_if<Container>)(elt));
             ^~~~~~~~~~~~~~~~~~~~~~

再次:

template<class T> T x{0};  // good
template<class T> auto x = T{0};  // bad

int main()
{
    return x<int> + x<long>;
}

bad.cc:6:19: error: invalid operands to binary expression ('auto' and 'auto')

1 个答案:

答案 0 :(得分:0)

Per @ dyp和@ ildjarn的评论:是的,这是Clang 3.4中的一个错误(也许是3.5)。它在Clang 3.7.0中修复(也许更早)。 Wandbox上提供了各种版本的Clang。

here is the C++14 code I was trying to construct!

#include <type_traits>

template <class T>
struct forward_wrapper {

    template <class U, class Enable = std::enable_if_t<(sizeof(std::remove_reference<U>), std::is_reference<T>::value)>>
    static U forward(U&& u) {
        return u;
    }

    template <class U, class Enable = std::enable_if_t<(sizeof(std::remove_reference<U>), !std::is_reference<T>::value)>>
    static decltype(auto) forward(U&& u) {
        return static_cast<typename std::remove_reference<U>::type &&>(u);
    }
};

template <class T> auto forward =
    [](auto&& u) -> decltype(auto) { return forward_wrapper<T>::forward(u); };

// --------

#include <stdio.h>
#include <vector>

void bar(int &) { puts("Called copy function"); }
void bar(int &&) { puts("Called move function"); }

template<class Container>
void foo(Container&& c) {
    for (auto&& elt : c) {
        bar(forward<Container>(elt));
    }
}

int main() {
    std::vector<int> v = {1,2};
    foo(v);
    foo(std::move(v));
}