MSVC 错误:具有 int64_t 成员的模板类 - '后跟 __int64 是非法的'

问题描述 投票:0回答:1

正在进行交叉编译 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

我尝试过的事情

  1. 使用显式
    std::
    命名空间:
inner_class<std::int64_t> _int64;
  1. 使用类型别名:
using int64_type = std::int64_t;
inner_class<int64_type> _int64;
  1. 添加 typename 关键字:
inner_class<typename std::int64_t> _int64;

都会产生相同或相似的错误。我做错了什么?这是 MSVC 特有的问题吗?

c++ templates visual-c++ visual-studio-2019 int64
1个回答
0
投票

在您的示例中

_int64
是一个实例变量。 MSVC 有一个具有该名称的类型,因此存在冲突。只需重命名变量即可:

inner_class<int64_t> int64;  // No error here
© www.soinside.com 2019 - 2024. All rights reserved.