Miles C.
Miles C.

Reputation: 111

Passing arguments by template -- for efficiency?

There are two standard methods of passing arguments -- by value, and constant reference. Each has its trade-offs, with value being preferable in most cases that the data is very small. However, I just recently looked into templates more, and the way I understand it, they act more like a macro. Could you use templates to favor efficiency (disregarding bad code cleanliness for now)? For example, if I passed a std::vector through a template, would it allow access to the current scope to the function called?

Upvotes: 1

Views: 142

Answers (1)

jwezorek
jwezorek

Reputation: 9525

I think you are misunderstanding what templates are.

Template arguments are not another way of passing runtime arguments to a function. Templates are a way essentially of doing code generation if you want to use the exact same code multiple times but with different types and/or constants when you know all the types and constant values at compile time.

So to answer your question

For example, if I passed a std::vector through a template, would it allow access to the current scope to the function called?

you can't pass an std::vector value to a template function beyond normal argument passing which may be parametrized on the type. As for allowing access to a vector in the current scope, you don't need templates for that anyway: pass a reference to the vector.

Upvotes: 2

Related Questions