[英]Function with default argument doesn't compile with VC++, but does with g++ and clang
I've searched this site and cannot find a solution to my problem.我已经搜索了这个网站,但找不到解决我的问题的方法。 I have reduced my sample code down as much as I think I can while still retaining the relevant error.
我已经尽可能地减少了我的示例代码,同时仍然保留了相关的错误。 I am left with the following two files:
我留下了以下两个文件:
test.hpp测试文件
namespace models {
template<typename FloatingPoint>
class ellipsoid {
public:
explicit ellipsoid(FloatingPoint = 6378137.0);
private:
FloatingPoint a_;
};
template<typename FloatingPoint>
ellipsoid<FloatingPoint>::ellipsoid(FloatingPoint a) : a_(a) {}
} // End namespace models
// Function declaration
template<typename FloatingPoint>
FloatingPoint compute(FloatingPoint,
const models::ellipsoid<FloatingPoint>& =
models::ellipsoid<FloatingPoint>());
// Function definition
template<typename FloatingPoint>
FloatingPoint compute(FloatingPoint x,
const models::ellipsoid<FloatingPoint>& model) {
return 3.14;
}
test.cpp测试.cpp
#include "test.hpp"
int main() {
compute(10.0);
return 0;
}
When I compile the above code using VC++ 2017, I get the following error:当我使用 VC++ 2017 编译上述代码时,出现以下错误:
error C2512: 'models::ellipsoid<FloatingPoint>': no appropriate default constructor available
note: No constructor could take the source type, or constructor overload resolution was ambiguous
Both clang and g++ compile this without a problem. clang 和 g++ 编译这个都没有问题。 Also, if I remove the
ellipsoid
class from the namespace models
, and remove the models::
invocations in the compute
function, it then compiles fine using VC++.此外,如果我从命名空间
models
删除ellipsoid
类,并删除compute
函数中的models::
调用,那么它可以使用 VC++ 进行编译。 Is this a bug within the VC++ compiler, or have I got a bug in my code?这是 VC++ 编译器中的错误,还是我的代码中有错误?
This was a bug in VC++, which was fixed in Visual Studio 2019 since compiler version 19.23.这是 VC++ 中的一个错误,自编译器版本 19.23 起已在 Visual Studio 2019 中修复。 Demo: https://gcc.godbolt.org/z/a5TxPa8ac
演示: https : //gcc.godbolt.org/z/a5TxPa8ac
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.