Query on c++ iterator tag -
i going through code snippet
template <class raiter> void alg(raiter, raiter, std::random_access_iterator_tag) { std::cout << "alg() called random-access iterator\n"; }
for first time, seeing data types in function parameter section (std::random_access_iterator_tag). used seeing
[std::random_access_iterator_tag rand_iter;]
this sort of representation allowed in templates not in non-templated functions.
two questions:
1) why data type name mentioned no variable of mentioned?
2) why allowed templated functions not non-templated functions?
actually that's not related templates @ all.
it's unnamed parameter , it's legal when:
- declaring method (so have no body)
- defining method won't use argument
basically can respect signature without having named argument @ costs, eg:
float foo(float, int, float); int main() { float x = foo(10.0f, 5, 20.0f); return 0; } float foo(float a, int, float b) { return a+b; }
this can useful in specific circumstances, think pure virtual
methods or forward declaration of methods.
in specific case helps suppress unused parameter warning, example:
float foo(float a, int z, float b) { return a+b; }
this yields warning -wunused-parameter
won't if explicitly state won't use removing name.
Comments
Post a Comment