1

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?

Miles C.
  • 111
  • 4
  • 6
    Your understanding of templates seems wrong. Function templates do not act like macros, they act like functions, just that it defines a function for each set of template arguments. You should learn about them from a decent resource, e.g. one the [recommended books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user17732522 Feb 04 '22 at 02:27
  • 2
    templates are a compile time feature. A Unless the value is known at compile time, you wont even have a chance to pass it as a non-type template parameter. – NathanOliver Feb 04 '22 at 02:42
  • To add to the above comments, templates allow the compiler to write code for you. It is the wrong way to make your code faster. – Dúthomhas Feb 04 '22 at 04:13
  • I’m voting to close this question because it's not actually a valid question. Specifically, based on an incorrect belief of what C++ templates are, the question is seeking to use templates in a manner for which they cannot be used. There are no workable ways of answering such a question, other than suggesting the OP learn what templates really are in C++. – Peter Feb 04 '22 at 05:57
  • You are beginner, so do not focus on such unimportant micro optimizations. You should be more focus on mastering basics, when you have more skills (lots of more) then you can wonder about such optimizations. – Marek R Feb 04 '22 at 16:54
  • “_Optimization_” is one of those words people like to throw around as if it were an important autobus-sized thing. You might want to read [When is optimization premature?](https://stackoverflow.com/questions/385506/when-is-optimisation-premature) Optimization belongs to _algorithms_ and to _benchmarking_. Everything else — _especially_ playing with language weirdness, should be defenestrated. Write correct, clean, readable code. Don’t be tricky. – Dúthomhas Feb 07 '22 at 22:18

1 Answers1

2

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.

jwezorek
  • 8,592
  • 1
  • 29
  • 46