In the definition of the function below the class definition, the second two "typename"s shouldn’t be there according to GCC and Clang, but it compiles on Visual Studio. Anybody know why? Compiling my code on different compilers is pretty hard when Visual Studio doesn’t seem to conform to the spec in a lot of ways.
#include <iostream>
class BaseClass { };
/* CLASS DEFINITION */
template <typename getter_t, typename setter_t>
class MyClass : public BaseClass
{
public:
using getter_return_t = int;
void reset_value_to(const getter_return_t&, const char* to_append);
};
/* FUNCTION DEFINITION */
template <typename getter_t, typename setter_t>
void MyClass<typename getter_t, typename setter_t>::reset_value_to(const getter_return_t& value, const char* to_append)
{
std::cout << "Yo";
}
int main()
{
MyClass<int, int> c;
c.reset_value_to(5, nullptr);
}
Clearly it’s wrong, but it compiles on Visual Studio on C++20, and I’ve disabled language extensions. Are there other options I can set to make Visual Studio compiler more conformant?
2
Answers
Based on the output from the compiler explorer, it seems the problem is here:
MSVC accepts that, but Clang does not.
It should probably just be this, without the extra
typename
keywords:MSVC, GCC, and Clang accept that.
Zebrafish. Yes, I can see the failure in GCC, but not in CLang. Not the MSVC2019 variant, anyway.
typename in template parameter declarations is a C++17 onwards feature. I think VS2019 language level 20 is right to accept it. What I can’t quite get, now is why GCC 13.2.0 fails do compile even with the -std=c++20 flag set….
Oh, it’s the -pedantic flag that apparently throws us back to C99.