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
- Using explicit
std::
namespace:
inner_class<std::int64_t> _int64;
- Using type aliases:
using int64_type = std::int64_t;
inner_class<int64_type> _int64;
- 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
In your example
_int64
is an instance variable. MSVC has a type with that name, so there’s a conflict. Just rename the variable:Add
/Za
flag for cl.exehttps://learn.microsoft.com/en-us/cpp/cpp/int8-int16-int32-int64?view=msvc-170