3 ms·
There is nothing simple about that (not to mention it's not actually true either).
by Kranar 5y ago
There is nothing simple about that (not to mention it's not actually true either).
- jcelerier 5y agoYes it is ? Quoting the standard: > If the placeholder-type-specifier is of the form type-constraint auto, the deduced type T' replacing T is determined using the rules for template argument deduction. https://eel.is/c++draft/dcl.spec.auto https://eel.is/c++draft/dcl.spec.auto Now if you program in C++ regularly and don't know that in template<typename T> void f(T t) { } f(something); does a copy of something, I don't know what to say - I have never met any professional c++ programmer not knowing the language works that way
- Kranar 5y agoYou need to continue reading that paragraph to the very end and note the exception for when an initializer list is used. The following are not equivalent nor do they apply the same rules: template<typename T> void f(T) {} f({1, 2, 3}); auto x = {1, 2, 3}; >Now if you program in C++ regularly and don't know that in... I program in C++ daily and no, I didn't know that a copy is made. What I do know, as an actual professional C++ programmer, is that whether a copy is performed is incredibly complex and depends on numerous factors such as whether a move constructor exists and "something" is an lvalue but not an rvalue reference, or if "something" is an rvalue (which is not the same as an rvalue reference) and T defines a move constructor. And finally if something is the same type as T and T has a copy constructor, then the final question is whether copy elision will be performed, which the standard specifies is a valid optimization even if said optimization would change the observable behavior of the program. That's what I... as a professional C++ programmer know but I fully admit that C++ is such a complex beast of a language that I am almost definitely missing a few corner cases.
- jcelerier 5y ago> You need to continue reading that paragraph to the very end and note the exception for when an initializer list is used. Exceptions don't mean that the general rule does not apply ? I don't know how it is possible to be more explicit that "the deduced type T' replacing T is determined using the rules for template argument deduction.". That'd be like saying that "priority to the right" when driving isn't a rule because there is sometimes a "stop". > I program in C++ daily and no, I didn't know that a copy is made. you're kidding > whether a copy is performed is incredibly complex and depends on numerous factors such as whether a move constructor exists and "something" is an lvalue but not an rvalue reference, or if "something" is an rvalue (which is not the same as an rvalue reference) and T defines a move constructor. but `something` cannot be a rvalue in f(something); maybe in f(something()); or f(some + thing); but just a variable named "something", as is, passed to a template argument or auto will necessarily lead to the creation of a new value of the same type. Whether it is copied, moved, materializes three different types because people forgot to mark their constructors explicit or whatever else frankly does not matter much in my experience (after all people survived for 30 years without move semantics) - what matters is whether a new value is created (which I personally and improperly call a "copy" no matter what - I should have been more precise but it's really the most useful distinction to make in practice imho as it means that some function will be called) or just a reference to existing value which is always a no-op no matter what. > And finally if something is the same type as T and T has a copy constructor, then the final question is whether copy elision will be performed, I've been at it for 15 years now and I don't remember one time in non-toy-example-code where copy elision taking place or not did matter. Thinking about is is a self-inflicted problem ; expect that a copy happens, if it's slow, profile and if it's not, be happy.