I have been confused by this for a while. Say if I want to call std::make_shared
I usually have to specify the type I am making using std::make_shared<MyClass>(...)
but I don't think angle brackets are needed in every situation. One example is the print
function for {fmt}
library: it is a template function, but when you call it you can do something like this: fmt::print("Elapsed time: {0:.2f} seconds", 1.23);
, without fully specifying the types. I am just wondering why the difference and what's the rule governing when an angle bracket is needed when calling a template function?
Asked
Active
Viewed 36 times
1
-
The compiler can oftenb *deduce* the template argument from the arguments to the function. But in the case of e.g. `std::make_shared` the arguments are not really related to the template type, so this deduction can't be done. – Some programmer dude Jul 01 '22 at 14:51
-
2Basically if the template types are used in the function arguments in an unambiguous way then you don't need to specify them, otherwise you do – Alan Birtles Jul 01 '22 at 14:57