#include <iostream>
#include <type_traits>
#include <typeinfo>
#include <vector>

template <class... T> struct list {};
template<class T> struct type_ { using type = T; };

namespace detail
{
  template<class, class>
  struct dup_append_list;
  template<template<class...> class List, class... Ts, class... Us>
  struct dup_append_list<List<Ts...>, List<Us...>>
  {
    using type = List<Ts..., Ts..., Us...>;
  };
  template<class T, template<class...> class List, std::size_t N>
  struct filled_list_impl
  : dup_append_list<
    typename filled_list_impl<T, List, N/2>::type,
    typename filled_list_impl<T, List, N - N/2*2>::type
  >
  {};
  template<class T, template<class...> class List>
  struct filled_list_impl<T, List, 1>
  {
    using type = List<T>;
  };
  template<class T, template<class...> class List>
  struct filled_list_impl<T, List, 0>
  {
    using type = List<>;
  };
}

template<class T, std::size_t N, template<class...> class List = list>
using filled_list = typename detail::filled_list_impl<T, List, N>::type;

namespace detail
{
  template<class T> struct element_at;
  
  template<class... Ts>
  struct element_at<list<Ts...>>
  {
    //previous version that worked
    template<class T> type_<T> static at(Ts..., type_<T>*, ...);
  };
    
  template<class T> T extract_type(type_<T>*);
  
  template<std::size_t N, class Seq> struct at_impl;
  
  //version that worked in CUDA 8
  template<std::size_t N, template<typename...> class L, class... Ts>
  struct at_impl<N,L<Ts...>>
     : decltype(element_at<filled_list<void const *, N>>::at(static_cast<type_<Ts>*>(nullptr)...))
  {
  };

}

template <class L, std::size_t Index>
using at_c = typename detail::at_impl<Index, L>::type;


int main(int, char*[])
{
	using vec_float = std::vector<float>;
	using l = list<float, vec_float, vec_float, vec_float>;
	using result_type = at_c<l, 1UL>;

	//should print out std::vector<float> type
	result_type temp {};
	std::cout << typeid(temp).name() << std::endl;
	return 0;
}

	.file	"tmpxft_0037fe44_00000000-6_main.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB4011:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE4011:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB4034:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE4034:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.text._ZNSt12_Vector_baseIfSaIfEED2Ev,"axG",@progbits,_ZNSt12_Vector_baseIfSaIfEED5Ev,comdat
	.align 2
	.weak	_ZNSt12_Vector_baseIfSaIfEED2Ev
	.type	_ZNSt12_Vector_baseIfSaIfEED2Ev, @function
_ZNSt12_Vector_baseIfSaIfEED2Ev:
.LFB4341:
	.cfi_startproc
	endbr64
	movq	(%rdi), %rax
	testq	%rax, %rax
	je	.L4
	movq	16(%rdi), %rsi
	movq	%rax, %rdi
	subq	%rax, %rsi
	jmp	_ZdlPvm@PLT
.L4:
	ret
	.cfi_endproc
.LFE4341:
	.size	_ZNSt12_Vector_baseIfSaIfEED2Ev, .-_ZNSt12_Vector_baseIfSaIfEED2Ev
	.weak	_ZNSt12_Vector_baseIfSaIfEED1Ev
	.set	_ZNSt12_Vector_baseIfSaIfEED1Ev,_ZNSt12_Vector_baseIfSaIfEED2Ev
	.section	.text.startup
	.globl	main
	.type	main, @function
main:
.LFB3998:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA3998
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	leaq	_ZTSSt6vectorIfSaIfEE(%rip), %rsi
	leaq	_ZSt4cout(%rip), %rdi
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	subq	$40, %rsp
	.cfi_def_cfa_offset 64
	movq	%fs:40, %rax
	movq	%rax, 24(%rsp)
	xorl	%eax, %eax
	xorl	%eax, %eax
	movq	%rsp, %rbp
	movq	%rax, (%rsp)
	movq	%rax, 8(%rsp)
	movq	%rax, 16(%rsp)
.LEHB0:
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	call	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@PLT
.LEHE0:
	movq	%rbp, %rdi
	call	_ZNSt12_Vector_baseIfSaIfEED2Ev
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	je	.L10
	jmp	.L12
.L11:
	endbr64
	movq	%rax, %rbx
.L8:
	movq	%rbp, %rdi
	call	_ZNSt12_Vector_baseIfSaIfEED2Ev
	movq	24(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L12
	movq	%rbx, %rdi
.LEHB1:
	call	_Unwind_Resume@PLT
.LEHE1:
.L12:
	call	__stack_chk_fail@PLT
.L10:
	addq	$40, %rsp
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3998:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA3998:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE3998-.LLSDACSB3998
.LLSDACSB3998:
	.uleb128 .LEHB0-.LFB3998
	.uleb128 .LEHE0-.LEHB0
	.uleb128 .L11-.LFB3998
	.uleb128 0
	.uleb128 .LEHB1-.LFB3998
	.uleb128 .LEHE1-.LEHB1
	.uleb128 0
	.uleb128 0
.LLSDACSE3998:
	.section	.text.startup
	.size	main, .-main
	.weak	_ZTSSt6vectorIfSaIfEE
	.section	.rodata._ZTSSt6vectorIfSaIfEE,"aG",@progbits,_ZTSSt6vectorIfSaIfEE,comdat
	.align 16
	.type	_ZTSSt6vectorIfSaIfEE, @object
	.size	_ZTSSt6vectorIfSaIfEE, 18
_ZTSSt6vectorIfSaIfEE:
	.string	"St6vectorIfSaIfEE"
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <iostream>
#include <type_traits>
#include <typeinfo>
#include <vector>

template <class... T> struct list {};
template<class T> struct type_ { using type = T; };

namespace detail
{
  template<class, class>
  struct dup_append_list;
  template<template<class...> class List, class... Ts, class... Us>
  struct dup_append_list<List<Ts...>, List<Us...>>
  {
    using type = List<Ts..., Ts..., Us...>;
  };
  template<class T, template<class...> class List, std::size_t N>
  struct filled_list_impl
  : dup_append_list<
    typename filled_list_impl<T, List, N/2>::type,
    typename filled_list_impl<T, List, N - N/2*2>::type
  >
  {};
  template<class T, template<class...> class List>
  struct filled_list_impl<T, List, 1>
  {
    using type = List<T>;
  };
  template<class T, template<class...> class List>
  struct filled_list_impl<T, List, 0>
  {
    using type = List<>;
  };
}

template<class T, std::size_t N, template<class...> class List = list>
using filled_list = typename detail::filled_list_impl<T, List, N>::type;

namespace detail
{
  template<class T> struct element_at;
  
  template<class... Ts>
  struct element_at<list<Ts...>>
  {
    //previous version that worked
    template<class T> type_<T> static at(Ts..., type_<T>*, ...);
  };
    
  template<class T> T extract_type(type_<T>*);
  
  template<std::size_t N, class Seq> struct at_impl;
  
  //version that worked in CUDA 8
  template<std::size_t N, template<typename...> class L, class... Ts>
  struct at_impl<N,L<Ts...>>
     : decltype(element_at<filled_list<void const *, N>>::at(static_cast<type_<Ts>*>(nullptr)...))
  {
  };

}

template <class L, std::size_t Index>
using at_c = typename detail::at_impl<Index, L>::type;


int main(int, char*[])
{
	using vec_float = std::vector<float>;
	using l = list<float, vec_float, vec_float, vec_float>;
	using result_type = at_c<l, 1UL>;

	//should print out std::vector<float> type
	result_type temp {};
	std::cout << typeid(temp).name() << std::endl;
	return 0;
}

	.text
	.file	"main.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:                                # %_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.exit
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	$_ZSt4cout, %ebx
	movl	$_ZSt4cout, %edi
	movl	$_ZTSSt6vectorIfSaIfEE, %esi
	movl	$17, %edx
	callq	_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l
	movq	_ZSt4cout(%rip), %rax
	movq	-24(%rax), %rdi
	addq	%rbx, %rdi
	movl	$10, %esi
	callq	_ZNKSt9basic_iosIcSt11char_traitsIcEE5widenEc
	movsbl	%al, %esi
	movl	$_ZSt4cout, %edi
	callq	_ZNSo3putEc
	movq	%rax, %rdi
	callq	_ZNSo5flushEv
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.type	_ZTSSt6vectorIfSaIfEE,@object   # @_ZTSSt6vectorIfSaIfEE
	.section	.rodata._ZTSSt6vectorIfSaIfEE,"aG",@progbits,_ZTSSt6vectorIfSaIfEE,comdat
	.weak	_ZTSSt6vectorIfSaIfEE
_ZTSSt6vectorIfSaIfEE:
	.asciz	"St6vectorIfSaIfEE"
	.size	_ZTSSt6vectorIfSaIfEE, 18

	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __gxx_personality_v0
	.addrsig_sym _ZSt4cout
	.addrsig_sym _ZTSSt6vectorIfSaIfEE
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
