简体   繁体   English

带有默认参数的函数不能用 VC++ 编译,但可以用 g++ 和 clang 编译

[英]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.

相关问题 为什么VC ++编译代码而clang没有? - Why does VC++ compile the code while clang doesn't? 函数指针的模板参数推导(g ++&ICC vs Clang ++&VC ++) - template argument deduction for function pointer (g++ & ICC vs Clang++ & VC++ ) 为什么VC ++会编译此代码,而Clang不会 - Why does VC++ compile this code while Clang won't 在C ++,g ++ / clang ++和vc ++中打印函数的地址,谁是对的? - Print an address of function in C++, g++/clang++ vs vc++ , who is right? `apply`模板在g ++中编译,但不在clang ++和vc ++中编译 - `apply` template compiles in g++ but not in clang++ and vc++ C ++朋友函数模板重载和SFINAE在clang ++,g ++,vc ++中的不同行为(C ++ 14模式) - C++ friend function template overloading and SFINAE different behaviors in clang++, g++, vc++ (C++14 mode) 为什么下面的代码不能在 MSVC 中编译而在 g++ 中编译? - Why the following code doesn't compile in MSVC and does in g++? g ++不会使用assert编译constexpr函数 - g++ doesn't compile constexpr function with assert in it VC ++与G ++,cout - VC++ vs. G++, cout wcscpy在g ++和vc ++中的不同行为 - Different behavior of wcscpy in g++ and vc++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM