我有一个用g++-5.2.0编译和运行的MVE程序,而不是clang-602.0.53。程序试图将lambda表达式分配给兼容类型的类型别名。
#include<iostream>
#include<complex>
using std::cout;
using std::endl;
using std::complex;
// type alias
using CfDD = complex<double> (*) (double);
// lambda of compatible type
auto theLambda = [] (double _) {return complex<double>({1,0});};
int main()
{ // Show that the lambda is callable:
cout << theLambda(3.14) << endl;
// Show that the lambda is assignable to a var of type CfDD
CfDD cfdd = theLambda;
cout << cfdd (3.14) << endl;
}该程序在g++-5.2.0编译时工作:
$ g++-5 --version
g++-5 (Homebrew gcc 5.2.0) 5.2.0
...
$ g++-5 -std=c++14 main.cpp && ./a.out
(1,0)
(1,0)但是产生了一个错误,我不明白,也不知道如何在clang下修复:
$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with- gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
...
$ gcc -std=c++14 main.cpp
main.cpp:10:40: error: ambiguous conversion for functional-style cast from 'void' to
'complex<double>'
auto theLambda = [] (double _) {return complex<double>({1,0});};
^~~~~~~~~~~~~~~~~~~~~
... candidate is the implicit move constructor
class _LIBCPP_TYPE_VIS_ONLY complex<double>
^
... candidate is the implicit copy constructor
candidate constructor
complex<double>::complex(const complex<float>& __c)这个错误意味着什么,为什么这段代码不编译?
发布于 2015-08-25 19:55:17
当你写到:
return complex<double>({1,0});我们查看std::complex的有效构造函数并发现:
constexpr complex(double re = 0.0, double im = 0.0); // (1)
constexpr complex( const complex& other ); // (2)
constexpr complex(const complex<float>& other); // (3a)
explicit constexpr complex(const complex<long double>& other); // (3b)我们正在使用初始化程序列表来构造这个对象。因此,(1)是不可行的,(3b)也不可行,因为该构造函数被标记为explicit。然而,另外两个都是可行的,因为complex<double>和complex<float>都可以由两个int来构造,两者都不是比另一个更好,这就是为什么clang抱怨“模棱两可的转换”。
最简单的解决方案是删除不必要的{}:
return complex<double>(1,0);请注意,您不需要将参数命名为_,您就不能为它提供名称。
https://stackoverflow.com/questions/32212597
复制相似问题