VS2017 模板特化错误 cannot convert from 'Class *(__cdecl *)(Args...)' to 'Class *(__cdecl *)(Args...)'
VS2017 template specialization error cannot convert from 'Class *(__cdecl *)(Args...)' to 'Class *(__cdecl *)(Args...)'
这是我要迁移到 VS2017 的代码库的简化版本。
以下代码在 VS2013 和 Intel C++ 编译器 2017 更新 4 中编译,但在 VS2013 中不编译。
#include <type_traits>
template<typename F, F f>
struct S1
{};
template<typename F, F f>
struct S2
{};
template<typename F, F f>
using BaseType = typename std::conditional<std::is_member_function_pointer<F>::value, S1<F, f>, S2<F, f>>::type;
template<typename Class, typename... Args>
Class * call_constructor(Args... args)
{
return new Class(args...);
}
template<class Class, typename... Args>
struct Constructor : BaseType<Class *(*)(Args...), call_constructor<Class, Args...>>
{
using ReturnType = Class *;
};
int main() {}
我在构造函数的定义上遇到错误 class:
main.cpp(20): error C2440: 'specialization': cannot convert from 'Class *(__cdecl *)(Args...)' to 'Class *(__cdecl *)(Args...)'
note: None of the functions with this name in scope match the target type
note: see reference to class template instantiation 'Constructor' being compiled
如果我直接从 S1 或 S2 继承构造函数,错误就会消失。所以我认为问题出在 std::conditional 定义上。
有什么想法吗?谢谢
在多次尝试重写代码后,我的一位同事得出了成功的版本:
template<typename F, F f>
struct BaseType_
{
using type = typename std::conditional<std::is_member_function_pointer<F>::value, S1<F, f>, S2<F, f>>::type;
};
template<typename F, F f>
using BaseType = typename BaseType_<F, f>::type;
template<class Class, typename... Args>
struct Constructor : BaseType_<Class*(*)(Args...), call_constructor<Class, Args...>>::type
{
using ReturnType = Class *;
};
也许这样可以通过延迟继承的实例化来解决问题
代码。我们不确定:)
这是我要迁移到 VS2017 的代码库的简化版本。 以下代码在 VS2013 和 Intel C++ 编译器 2017 更新 4 中编译,但在 VS2013 中不编译。
#include <type_traits>
template<typename F, F f>
struct S1
{};
template<typename F, F f>
struct S2
{};
template<typename F, F f>
using BaseType = typename std::conditional<std::is_member_function_pointer<F>::value, S1<F, f>, S2<F, f>>::type;
template<typename Class, typename... Args>
Class * call_constructor(Args... args)
{
return new Class(args...);
}
template<class Class, typename... Args>
struct Constructor : BaseType<Class *(*)(Args...), call_constructor<Class, Args...>>
{
using ReturnType = Class *;
};
int main() {}
我在构造函数的定义上遇到错误 class:
main.cpp(20): error C2440: 'specialization': cannot convert from 'Class *(__cdecl *)(Args...)' to 'Class *(__cdecl *)(Args...)' note: None of the functions with this name in scope match the target type note: see reference to class template instantiation 'Constructor' being compiled
如果我直接从 S1 或 S2 继承构造函数,错误就会消失。所以我认为问题出在 std::conditional 定义上。
有什么想法吗?谢谢
在多次尝试重写代码后,我的一位同事得出了成功的版本:
template<typename F, F f>
struct BaseType_
{
using type = typename std::conditional<std::is_member_function_pointer<F>::value, S1<F, f>, S2<F, f>>::type;
};
template<typename F, F f>
using BaseType = typename BaseType_<F, f>::type;
template<class Class, typename... Args>
struct Constructor : BaseType_<Class*(*)(Args...), call_constructor<Class, Args...>>::type
{
using ReturnType = Class *;
};
也许这样可以通过延迟继承的实例化来解决问题 代码。我们不确定:)