次のテンプレート定義とテンプレートの特殊化定義がどのように機能するのかを理解するのは難しいですか?私には、factorial<34>
またはfactorial<T-1>
が奇妙に見えます!例えばテンプレート定義の階乗<T - 1>の意味
:
factorial<T - 1>::value
は何を意味?
#include <iostream>
template<int T>
struct factorial {
enum { value = factorial<T - 1>::value * T };
};
template<>
struct factorial<1> {
enum { value = 1 };
};
int main()
{
std::cout << factorial<34>::value << std::endl;
}
g++ -o testSTL01 testSTL01.cpp -Wall
testSTL01.cpp: In instantiation of ‘factorial<13>’:
testSTL01.cpp:5:3: instantiated from ‘factorial<14>’
testSTL01.cpp:5:3: instantiated from ‘factorial<15>’
testSTL01.cpp:5:3: instantiated from ‘factorial<16>’
testSTL01.cpp:5:3: instantiated from ‘factorial<17>’
testSTL01.cpp:5:3: instantiated from ‘factorial<18>’
testSTL01.cpp:5:3: [ skipping 11 instantiation contexts ]
testSTL01.cpp:5:3: instantiated from ‘factorial<30>’
testSTL01.cpp:5:3: instantiated from ‘factorial<31>’
testSTL01.cpp:5:3: instantiated from ‘factorial<32>’
testSTL01.cpp:5:3: instantiated from ‘factorial<33>’
testSTL01.cpp:5:3: instantiated from ‘factorial<34>’
testSTL01.cpp:15:29: instantiated from here
testSTL01.cpp:5:3: warning: integer overflow in expression
start to run the app ...
0
これはかなり簡単です。どの部分を理解していないのですか? – Mankarse
人は*タイプ*のパラメータだけを持つテンプレートを使ってテンプレートに導入されるかもしれません。私はそれが、新しいもので奇妙な価値観の考え方だと思う。 –
何が問題なのですか? –