我正在尝试别名位于基类体内的类中的类型。
示例代码:
#include <iostream>
#include <typeinfo>
#include <type_traits>
template <typename...Ts>
struct X;
template <typename T, typename...Ts>
struct X<T, Ts...> : X<Ts...>
{
using base = X<Ts...>;
template <typename R, int I=0>
struct find
{
using type = base::template find<T, I+1>;
};
template <int I>
struct find<T, I>
{
using type = X;
};
}
template <>
struct X<>
{};
int main()
{
using type = X<int, float>;
std::cout << typeid(type::find<float>).name();
}
这为行生成了错误error: expected a type
:
using type = base::template find<T, I+1>;
我做错了什么?
答案 0 :(得分:4)
你错过了一个typename,因为base :: find是一个依赖类型:
#include <iostream>
#include <typeinfo>
#include <type_traits>
template <typename...Ts>
struct X;
template <typename T, typename...Ts>
struct X<T, Ts...> : X<Ts...>
{
using base = X<Ts...>;
template <typename R, int I=0>
struct find
{
using type = typename base::template find<T, I+1>;
};
template <int I>
struct find<T, I>
{
using type = X;
};
};
template <>
struct X<>
{};
int main()
{
using type = X<int, float>;
std::cout << typeid(type::find<float>).name();
}
但是还有一些工作要做,检查输出。