我写了一些代码,在最新版本的GCC和Clang上编译良好,但在MSVC上失败:
无效的模板参数用于模板参数'TL',需要一个类模板。
请问这是个错误还是我理解错了?
如果没有对types_list进行部分特化,则在MSVC上也可以正常工作。
无效的模板参数用于模板参数'TL',需要一个类模板。
请问这是个错误还是我理解错了?
如果没有对types_list进行部分特化,则在MSVC上也可以正常工作。
#include <cstdint>
#include <type_traits>
namespace detail
{
template <std::size_t Index, typename ...Ts>
struct get
{
static_assert(Index < sizeof...(Ts), "types_list::get index out of bounds");
private:
template <std::size_t CurrentIndex, typename ...Us>
struct helper
{
using type = void;
};
template <std::size_t CurrentIndex, typename U, typename ...Us>
struct helper<CurrentIndex, U, Us...>
{
using type = std::conditional_t<CurrentIndex == Index, U, typename helper<CurrentIndex + 1, Us...>::type>;
};
public:
using type = typename helper<0, Ts...>::type;
};
template <template <typename...> typename TL, typename ...Ts>
struct list_impl
{
inline static constexpr std::size_t size = sizeof...(Ts);
template <std::size_t Index>
using get = typename detail::get<Index, Ts...>::type;
};
}
template <typename ...Ts>
struct types_list : public detail::list_impl<types_list, Ts...>
{
};
template <typename T, typename ...Ts>
struct types_list<T, Ts...> : public detail::list_impl<types_list, T, Ts...>
{
private:
using impl = detail::list_impl<types_list, T, Ts...>;
public:
using front = typename impl:: template get<0>;
using back = typename impl:: template get<impl::size - 1>;
};
using t = types_list<int, double>::front;
using t2 = types_list<int, double>::back;
using t3 = types_list<int, char, double>::get<1>;
int main()
{
t x = 10;
t2 y = 1.4;
t3 z = 'a';
}
编辑:更详细的示例https://pastebin.com/snRC0EPi
/permissive
的其他值? - Davis Herringpush_back
/push_front
/ .. 并使用 TL。这里的主要思想是空列表没有back
和front
成员,但其他逻辑应该被重用。请查看更详细的版本(带有 push_back):https://pastebin.com/snRC0EPi - d_kog