skip to Main Content

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


  1. Based on the output from the compiler explorer, it seems the problem is here:

    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)
    

    MSVC accepts that, but Clang does not.

    It should probably just be this, without the extra typename keywords:

    template <typename getter_t, typename setter_t>
    void MyClass<getter_t, setter_t>::reset_value_to(const getter_return_t& value, const char* to_append)
    

    MSVC, GCC, and Clang accept that.

    Login or Signup to reply.
  2. 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.

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