(MSVC)在类之外实现模板类的模板方法时,C2244:无法将函数定义与现有声明匹配



遇到一些代码在GCC和Clang上编译的问题,但不是MSVC (VS2022)。似乎与此类似,但那是几年前的事了,这个问题是由方法模板中使用derived_from_specialization_of引起的,所以我不确定它是否是同样的问题。

我想知道这是什么原因,如果问题是我的代码或MSVC。

演示的例子:

#include <concepts>
template <template <class...> class Template, class... Args>
void derived_from_specialization_impl(const Template<Args...>&);
template <class T, template <class...> class Template>
concept derived_from_specialization_of = requires(const T& t) {
derived_from_specialization_impl<Template>(t);
};
template <class T>
class A
{
public:
A() = default;
};
template <derived_from_specialization_of<A> T>
class B;
template <derived_from_specialization_of<B> T>
class C
{
public:
C() = default;
};
template <derived_from_specialization_of<A> T>
class B
{
public:
B() = default;
template <derived_from_specialization_of<B> U>
C<U> Foo();
template <derived_from_specialization_of<B> U>
C<U> Bar()
{
return C<U>();
}
};
template <derived_from_specialization_of<A> T>
template <derived_from_specialization_of<B> U>
C<U> B<T>::Foo() 
{
return C<U>();
}

这是什么原因,如果问题是我的代码或MSVC。

这似乎是MSVC中的一个bug。您已经正确地提供了类外定义成员函数模板Foo<>

使用auto尾随返回类型似乎不能解决msvc中的问题。你可以为此提交一个bug报告。