【发布时间】:2019-05-06 01:05:28
【问题描述】:
我需要一个模板来找出类从其基类及其索引继承的类型顺序。该代码适用于 clang 和 gcc,但在作为目标环境的 Visual Studio 中,我收到内部编译器错误“致命错误 C1001:编译器中发生内部错误。”。我正在寻找一些解决方法,或者我的代码中可能有错误。是的,我已经尝试过 google。
谢谢,提前。
#include <type_traits>
#include <iostream>
struct BaseA
{
};
struct BaseB
{
};
struct BaseC
{
};
template <class... Types>
class type_list {};
template<typename Type, typename TypeList>
struct get_idx_for_type;
template<typename Type, template<typename...> typename TypeList, typename ...Types>
struct get_idx_for_type<Type, TypeList<Types...>>
{
template<int I, typename T, typename ...Rest>
struct find_type;
template<int I, typename T, typename U, typename ...Rest>
struct find_type< I, T, U, Rest... >
{
// problematic line for compiler, problem is somewhere in find_type recursion
static constexpr int value = std::is_same<T, U>::value ? I : find_type<I + 1, T, Rest...>::value;
};
template<int I, typename T, typename U>
struct find_type< I, T, U >
{
static constexpr int value = std::is_same<T, U>::value ? I : -1;
};
static constexpr int value = find_type<0, Type, Types...>::value;
};
template<typename ...Bases>
struct Foo : public Bases...
{
using base_types_list = type_list<Bases...>;
};
int main()
{
using T = Foo<BaseA, BaseB, BaseC>;
Foo<BaseA, BaseB, BaseC> q;
int a = get_idx_for_type<BaseA, T::base_types_list>::value;
std::cout << a << std::endl;
return 0;
}
【问题讨论】:
-
如果您使用的是最新版本的 Visual Studio,我建议您按照错误消息中的说明提交错误报告,这些天它们通常会很快得到修复
-
我在
C++ primer这本书学习,我在vs2017练习variadic template时遇到了同样的问题
标签: c++ visual-studio templates variadic-templates