2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

(C++テンプレート)関数テンプレートでの再帰について

Last updated at Posted at 2019-11-13

以下のように再帰させようとすると、失敗する。
関数テンプレートでは、テンプレート引数による再帰はできない模様。



template<int N,class T,typename std::enable_if<(N==0)>::type* = nullptr>
auto add_pointers(T) -> T*; 
   
template<int N,class T,typename std::enable_if<(N > 0)>::type* = nullptr>
auto add_pointers(T) -> decltype(add_pointers<N-1>(std::declval<T*>()));
   
int main()
{  
   
  decltype(add_pointers<0>(std::declval<int>())) val1;
  std::cout<<typeid(val1).name()<<std::endl;
   
  decltype(add_pointers<1>(std::declval<int>())) val2;
  std::cout<<typeid(val2).name()<<std::endl;
   
 //以下をコメントアウトすると、コンパイルが通る
  decltype(add_pointers<2>(std::declval<int>())) val3;
  std::cout<<typeid(val3).name()<<std::endl;                                                                                                                                                                                                                                     
} 

以下のように、関数の引数の型を変えれば、再帰させることが可能。

template<int N>
struct int_{};

template<int N,class T,typename std::enable_if<(N > 0)>::type* = nullptr>
auto add_pointers(T,int_<N>) -> decltype(add_pointers(std::declval<T*>(),int_<N-1>()));
 
template<int N,class T,typename std::enable_if<(N==0)>::type* = nullptr>
auto add_pointers(T,int_<N>) ->T;

int main()
{
 
  decltype(add_pointers(std::declval<int>(),int_<3>())) a;
  std::cout<<typeid(a).name()<<std::endl;
}

2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?