c z
c z

Reputation: 9037

C2244: 'MyTemplateClass<T>::MyFunction': unable to match function definition to an existing declaration

Could someone explain why the following code compiles on GCC, but not in Visual Studio.

I get error C2244: 'MyTemplate::List': unable to match function definition to an existing declaration at the noted line.

#include <array>

enum class MyEnum
{
    MAX = 5,
};

template<typename E> 
class MyTemplate
{
public :
    static const int NUMBER = static_cast<int>(E::MAX);
    std::array<int, NUMBER> List(); 
};

template<typename E>
std::array<int, MyTemplate<E>::NUMBER> MyTemplate<E>::List() // ERROR
{
    return std::array<int, MyTemplate<E>::NUMBER>();
}

int main()
{
    MyTemplate<MyEnum> myClass;
}

I presume it's something non-standard I'm doing that GCC allows and VS doesn't, but I can't figure out what. CLion is also happy with the code.

Given this answer it's also possible it's a bug in VS, although the specifics are different, the error message is the same. In my case the template seems simple however, involving one parameter and no nesting.

Upvotes: 0

Views: 101

Answers (1)

Jarod42
Jarod42

Reputation: 218288

As workaround, you might use:

template<typename E>
auto MyTemplate<E>::List()
-> std::array<int, NUMBER>
{
    return std::array<int, MyTemplate<E>::NUMBER>();
}

Demo

Upvotes: 1

Related Questions