【发布时间】:2020-06-26 20:14:19
【问题描述】:
我在 Visual Studio 中编写了一些 c++ 代码,并试图在我的 linux 服务器上的 c++ 代码上运行它。然而,当我尝试用 G++ 编译它时,它编译失败并出现大量错误。我查看了错误并能够将问题简化为:
template<int x>
struct Struct
{
template<int y>
static void F()
{
//Struct<x>::F<0>(); // compiles
//Struct<y>::G(); // compiles
Struct<y>::F<0>(); // does not compile?
}
static void G()
{
}
};
int main ()
{
Struct<0>::F<0>();
}
在 Visual Studio 上,这段代码编译得很好,但在 G++ 或 Clang++ 上,它编译失败。 G++ 8.3.0 上的错误:
test.cpp: In static member function ‘static void Struct<x>::F()’:
test.cpp:9:19: error: expected primary-expression before ‘)’ token
Struct<y>::F<0>(); // does not compile?
^
test.cpp: In instantiation of ‘static void Struct<x>::F() [with int y = 0; int x = 0]’:
test.cpp:19:18: required from here
test.cpp:9:15: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator<’
Struct<y>::F<0>(); // does not compile?
Clang++ 的错误:
5691311/source.cpp:9:19: error: expected expression
Struct<y>::F<0>(); // does not compile?
^
现场观看:https://rextester.com/AAL19278
您可以更改编译器并复制代码以查看不同的错误。
我是否可以解决这个问题,以便我的代码可以在 G++ 或 Clang++ 上编译?
原代码:
template<int x, int y>
ThisType add()
{
return ThisType::Create(this->x() + x, this->y() + y);
}
ResultPos to = p.add<0, 1>();
【问题讨论】:
-
VS-2019 不会在选择“一致性模式”时编译您的代码 (
/permissive-)。
标签: c++ visual-c++ g++ clang++