Sunday, 15 January 2012

c++ - Metaprogramming sample from Wikipedia -



c++ - Metaprogramming sample from Wikipedia -

trying understand sample wikipedia

template <int length> vector<length>& vector<length>::operator+=(const vector<length>& rhs) { (int = 0; < length; ++i) value[i] += rhs.value[i]; homecoming *this; } template <> vector<2>& vector<2>::operator+=(const vector<2>& rhs) { value[0] += rhs.value[0]; value[1] += rhs.value[1]; homecoming *this; }

firsttly can't compile @ all. got error syntax error : missing ';' before '<'

and sec question how utilize it? far understand template name vector not same std::vector.

this (a small) part of vector class, showing concept teaching purposes.

it cannot compiled alone. you'll have write rest of class.

then you'll have write calling code instantiates template , calls operators test it. debug (or add together trace messages) show right overload resolved.

note says "the next code may produced". don't need sec code. shows optimizer compiler can code.

c++ metaprogramming template-meta-programming

No comments:

Post a Comment