skip to Main Content

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?

2

Answers


  1. 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
    
    Login or Signup to reply.
  2. by flags/options for cl.exe?

    Add /Za flag for cl.exe

    https://learn.microsoft.com/en-us/cpp/cpp/int8-int16-int32-int64?view=msvc-170

    For compatibility with previous versions, _int8, _int16, _int32, and
    _int64 are synonyms for __int8, __int16, __int32, and __int64 unless compiler option /Za (Disable language extensions) is specified.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search