0
1

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 3 years have passed since last update.

(C++テンプレート)一部パラメータの明示について

Last updated at Posted at 2019-11-13

###以下のように一部パラメータだけ明示するのは、部分特殊化の範疇らしい。

template<int N,class T>
void func1();    
                 
template<class T>
void func1<0,T>();                                                                                                                                                                                                                                                                 
   
// 以下のように、完全な明示特殊化は可能              
//template<>     
//void func1<0,int>();

関数テンプレートでは部分特殊化ができないため、以下のようなエラーメッセージが出る。

error: non-type partial specialization ‘func1<0, T>’ is not allowed
 void func1<0,T>();

構造体では、部分特殊化が可能なため、以下のように書ける。


template<int N,class T>
struct struct1{
  static const int value = 1;
}; 
   
template<class T>
struct struct1<2,T>{
  static const int value = 2;
}; 

int main()
{  
   
  std::cout<<struct1<1,int>::value<<std::endl;                                                                                                                                                                                                                                     
  std::cout<<struct1<2,int>::value<<std::endl;
  
}

###関数テンプレートの型推定は、一部パラメータだけに作用させることが可能

template<int N,class T>
void func1(T val){                                                                                                                                                                                                                                                                 
  std::cout<<N<<" "<<val<<std::endl;
  };

int main()
{  
   
  int val = 10;
  func1<5>(val);
}
0
1
0

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?