gpt4 book ai didi

c++ - 调用的对象类型 'auto' 不是函数或函数指针

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:51:15 24 4
gpt4 key购买 nike

当我尝试编译此使用 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 通用 lambda 的编译器,因此我只测试过 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')

最佳答案

根据@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));
}

关于c++ - 调用的对象类型 'auto' 不是函数或函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28386407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com