正在进行交叉编译 this FLOSS,我正在尝试编译一个使用
int64_t
作为模板参数的简单模板类,但是 MSVC (Visual Studio 2019) 给了我有关非法类型使用的错误。这是一个最小的例子:
#include <cstdint>
class test_class {
public:
template<typename T>
class inner_class {
private:
T value;
public:
inner_class() : value(T()) {}
bool operator==(const inner_class<T>& other) const {
return value == other.value;
}
};
inner_class<int64_t> _int64; // Error here
};
int main() {
test_class t;
return 0;
}
编译时:
cl.exe /std:c++17 /EHsc /W4 test.cpp
我收到这些错误:
error C2628: 'test_class::inner_class<int64_t>' followed by '__int64' is illegal (did you forget a ';'?)
error C2208: 'test_class::inner_class<int64_t>': no members defined using this type
我尝试显式使用
std::int64_t
并尝试创建类型别名,但仍然遇到类似的错误。该代码可以使用 GCC 和 Clang 很好地编译。
Visual Studio 版本:
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30156 for x64
std::
命名空间:inner_class<std::int64_t> _int64;
using int64_type = std::int64_t;
inner_class<int64_type> _int64;
inner_class<typename std::int64_t> _int64;
都会产生相同或相似的错误。我做错了什么?这是 MSVC 特有的问题吗?
在您的示例中
_int64
是一个实例变量。 MSVC 有一个具有该名称的类型,因此存在冲突。只需重命名变量即可:
inner_class<int64_t> int64; // No error here