可变参数模板类型为 return 类型,MSVC 怪异
Variadic templated type as return type, MSVC weirdness
给定以下代码:
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;
}
也可以在这里找到:https://godbolt.org/z/8747rj77K
为什么它在 clang/gcc 而不是 msvc 上编译。
为什么 AnOtherGetAThing() 编译而不是 GetAThing()(在 msvc 上)。
msvc返回的错误是:
<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)'
谢谢
我会说 msvc 错误,
作为解决方法,您可以使用尾随 return 类型:
template <typename T>
template <typename U, typename... Args>
auto DummyKO<T>::GetAThing() -> AThing<U, Args...>
{
// ...
}
给定以下代码:
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;
}
也可以在这里找到:https://godbolt.org/z/8747rj77K
为什么它在 clang/gcc 而不是 msvc 上编译。 为什么 AnOtherGetAThing() 编译而不是 GetAThing()(在 msvc 上)。
msvc返回的错误是:
<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)'
谢谢
我会说 msvc 错误,
作为解决方法,您可以使用尾随 return 类型:
template <typename T>
template <typename U, typename... Args>
auto DummyKO<T>::GetAThing() -> AThing<U, Args...>
{
// ...
}