2
当我尝试编译使用lambda类型的变量模板的C++ 14代码时,从Clang(3.4和3.5)中看到一个奇怪的错误。被调用的对象类型'auto'不是函数或函数指针
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));
}
我从锵第二代码中看到的错误是:
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的编译器,所以Clan克是我测试过的。
这是我的错误还是叮当的bug?
我倾向于认为它是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')
在你的问题的代码片断编译罚款(后更改'class'到'struct'可访问)与最新版本的铿锵++。这表明你看到的错误是一个已修复的编译器错误。 – dyp 2015-02-07 19:42:16
@dyp Hooray!你能告诉我'clang --version'的输出吗?理想情况下,我想找到一个在线服务,如ideone或godbolt,支持该版本的Clang。 (http://ideone.com/jRZ5SD甚至不支持变量模板。) – Quuxplusone 2015-02-07 20:24:34
[melpon.org/wandbox](http://melpon.org/wandbox)通常具有非常新的g ++和clang ++版本,但我不确定您是否可以将编译器开关调整为合理的值(如'-std = C++ 1y') - 编辑:啊,您可以在页面左下方调整它。 [coliru.stacked-crooked.com](http:// http://coliru.stacked-crooked.com/)也具有g ++和clang ++的合理近期版本。我已经使用了一个铿锵声3.7.0版本(中继线228504),它应该是中继端的atm。 – dyp 2015-02-07 20:33:11