c++templatesvisual-c++visual-studio-2019int64

MSVC Error: Template class with int64_t member - 'followed by __int64 is illegal'


Working on cross compiling this FLOSS, I'm trying to compile a simple template class that uses int64_t as a template parameter, but MSVC (Visual Studio 2019) gives me errors about illegal type usage. Here's a minimal example:

#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;
}

When compiling with:

cl.exe /std:c++17 /EHsc /W4 test.cpp

I get these errors:

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

I've tried using std::int64_t explicitly and also tried creating type aliases, but still get similar errors. The code compiles fine with GCC and Clang.

Visual Studio version:

Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30156 for x64

What I've Tried

  1. Using explicit std:: namespace:
inner_class<std::int64_t> _int64;
  1. Using type aliases:
using int64_type = std::int64_t;
inner_class<int64_type> _int64;
  1. Adding typename keyword:
inner_class<typename std::int64_t> _int64;

All produce the same or similar errors. What am I doing wrong? Is this a MSVC-specific issue?


Solution

  • In your example _int64 is an instance variable. MSVC has a type with that name, so there's a conflict. Just rename the variable:

    inner_class<int64_t> int64;  // No error here
    

    This is what MSVC sees when you use try to use _int64 as a variable name:

    typedef long long __int64;
    typedef __int64 _int64;
    inner_class<int64_t> _int64; // _int64 == long long **error**