C++ 使用complex<;双倍>;
我有一个用g++-5.2.0编译和运行的MVE程序,但不是用clang-602.0.53。程序尝试将lambda表达式分配给兼容类型的类型别名C++ 使用complex<;双倍>;,c++,lambda,g++,clang,c++14,C++,Lambda,G++,Clang,C++14,我有一个用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 o
#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;
}
但产生了一个错误,我不理解,也不知道如何在叮当声下修复:
$ 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)
$gcc--版本
配置为:--prefix=/Applications/Xcode.app/Contents/Developer/usr--with-gxx include dir=/usr/include/c++/4.2.1
苹果LLVM版本6.1.0(clang-602.0.53)(基于LLVM 3.6.0svn)
...
$gcc-std=c++14 main.cpp
main.cpp:10:40:错误:函数样式从“void”转换为
“复杂”
auto-theLambda=[](双{返回复数({1,0});};
^~~~~~~~~~~~~~~~~~~~~
... 候选对象是隐式移动构造函数
类_LIBCPP_TYPE_VIS_ONLY complex
^
... 候选对象是隐式复制构造函数
候选构造函数
复杂的:复杂的(常数复杂的&u__c)
这个错误是什么意思?为什么这段代码不能编译?当您编写:
return complex<double>({1,0});
请注意,您不需要命名参数
。
,您不能为它提供名称。我由此推断{1,0}
的类型为void,因为编译器抱怨将void
转换为复杂的,正确吗?@Reb.cab错误的关键部分是不明确的。表达式的类型无关紧要。@Reb.cab括号内的列表是语法元素,不是完全成熟的表达式,不能说它们有类型。
return complex<double>({1,0});
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)
return complex<double>(1,0);