2017-06-07 21 views
-2

C++テンプレートではtemplate<typename T, T*>の意味は何ですか? そして、どのような状況で、私はそれを使用する必要がありますか?C++テンプレートのテンプレート<typename T、T *>の意味は?

#include <iostream> 
using namespace std; 

template<typename T, T*> 
void test(T a) 
{ 
    cout << "test template\n"; 
} 

int main(int argc, char **argv) 
{ 
    test(10); 
    return 0; 
} 

私は上記のコードからコンパイルエラーが発生しました。

./test.cpp: In function ‘int main(int, char**)’: 
./test.cpp:12: error: no matching function for call to ‘test(int)’ 

変更test(10);test<int, int*>(10);に、まだ動作しません。

+2

それはhttp://en.cppreference.com/w/cpp/language/template_parameters#([*非型テンプレートパラメータ*]と呼ばれていますNon-type_template_parameter) – NathanOliver

+0

@ NathanOliverテンプレート関数テストを呼び出すにはどうすればよいですか? –

答えて

2

使い方は次のようになります。

extern int global; 

int main() 
{ 
    test<int, &global>(10); 
} 

Demo

関連する問題