Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fa1a1fd

Browse files
Add files via upload
1 parent 7df2d88 commit fa1a1fd

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
template<typename T>
5+
void print_type(){ std::cout<<__PRETTY_FUNCTION__<<'\n'; }//i.e. for debugging
6+
7+
template<typename type,size_t size>
8+
struct array_adapter{
9+
type data[size];
10+
};
11+
12+
/************* testing runtime parameter deduction for template-argument ****************/
13+
/*NOTES:
14+
1) This technique basically allows us to use run-time parameters with compile-time
15+
2) C++17 generic-lamba expression can also be used inside 'if' statement to make the function generic
16+
*/
17+
template<size_t>
18+
void access_nth_tpp(size_t){}//i.e. base-case condition overload method
19+
20+
template<size_t N,typename type,typename... types>
21+
void access_nth_tpp(const size_t& index){//i.e. tpp -> template parameter-pack
22+
if(index==N){//i.e. perform any operation inside if statement to make conversion
23+
24+
constexpr size_t Compile_Time_Coverted=N;//i.e. index == N , means any operation u want to do with 'index', can also be done with 'N'
25+
26+
array_adapter<type,N*N> temp;//i.e. declares an array of N*N size perfectly, since both parameters are compile-time
27+
// array_adapter<type,index>();i.e. gives error, since its a run-time parameter
28+
29+
print_type<type>();//i.e. access nth-type of parameter-pack at run-time with this method
30+
return;
31+
}
32+
access_nth_tpp<N+1,types...>(index);
33+
}
34+
35+
template<typename... types>
36+
void Get_Access(size_t index){ access_nth_tpp<0,types...>(index); }
37+
38+
int main(){
39+
int index=2;
40+
Get_Access<int,char,std::string,bool>(index);//i.e. prints respective type
41+
return 0;
42+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#if __cplusplus >= 201103L
2+
/* A simple Work-around for deducing Nth-type in Template Parameter-Pack (at compile-time) */
3+
4+
template<int N,typename... T>
5+
struct get_nth_type;//i.e. forward declaration of primary template struct
6+
7+
template<typename T0,typename... T>
8+
struct get_nth_type<0,T0,T...>{//i.e. partially specialized struct template
9+
typedef T0 type;
10+
};
11+
template<int N,typename T0,typename... T>
12+
struct get_nth_type<N,T0,T...>{//i.e. partially specialized struct template
13+
typedef typename get_nth_type<N-1,T...>::type type;
14+
};
15+
16+
template<int N,typename... T>
17+
using get_type=typename get_nth_type<N,T...>::type;//i.e. simplifying previous formatted name
18+
//Using Format: get_type<1,string,int,bool,char> -> returns int type
19+
20+
#endif

‎Miscellaneous/print_type_func.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include<iostream>
2+
3+
#if __cplusplus >= 201103L
4+
template<typename... T>
5+
#else
6+
template<typename T>
7+
#endif
8+
void print_type(){ std::cout<<__PRETTY_FUNCTION__<<'\n'; }
9+
//Note: it also doesn't discard cv-qualifiers
10+
11+
int main(){
12+
print_type<const char*>();
13+
return 0;
14+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /