Home > Blockchain >  Variadic templated type as return type, MSVC weirdness
Variadic templated type as return type, MSVC weirdness

Time:11-02

Given the following code:

class DummyOK {
   public:
    template <typename U, typename... Args>
    class AThing {
       public:
    };

   public:
    template <typename U, typename... Args>
    AThing<U, Args...> GetAThing();
};

template <typename U, typename... Args>
typename DummyOK::template AThing<U, Args...> DummyOK::GetAThing() {
    return AThing<U, Args...>{};
}

template <typename T>
class DummyKO {
   public:
    template <typename U, typename... Args>
    class AThing {
       public:
    };

   public:
    template <typename U, typename... Args>
    AThing<U, Args...> GetAThing();

    template <typename U, typename... Args>
    AThing<U, Args...> AnOtherGetAThing() {
        return AThing<U, Args...>{};
    }
};

template <typename T>
template <typename U, typename... Args>
typename DummyKO<T>::template AThing<U, Args...> DummyKO<T>::GetAThing() {
    return AThing<U, Args...>{};
}

int main() {
    DummyOK{}.GetAThing<char, unsigned, float>();
    DummyKO<int>{}.GetAThing<char, unsigned, float>();
    DummyKO<int>{}.AnOtherGetAThing<char, unsigned, float>();

    return 0;
}

Also available here: https://godbolt.org/z/8747rj77K

Why does it compiles on clang/gcc but not msvc. Why does AnOtherGetAThing() compiles but not GetAThing() (on msvc).

The error returned by msvc is:

<source>(39): error C2244: 'DummyKO<T>::GetAThing': unable to match function definition to an existing declaration
<source>(39): note: see declaration of 'DummyKO<T>::GetAThing'
<source>(39): note: definition
<source>(39): note: 'DummyKO<T>::AThing<U,Args...> DummyKO<T>::GetAThing(void)'
<source>(39): note: existing declarations
<source>(39): note: 'DummyKO<T>::AThing<U,Args...> DummyKO<T>::GetAThing(void)'

Thanks

CodePudding user response:

I would say msvc bug,

as workaround, you might use trailing return type:

template <typename T>
template <typename U, typename... Args>
auto DummyKO<T>::GetAThing() -> AThing<U, Args...>
{
    // ...
}

Demo

  • Related