Alex Rivera | Logout

In which cases one needs to specify the template's argument `types` specifically?

Asked 2011-08-29T05:37:48.320
9
// Function declaration.
template <typename T1, 
          typename T2, 
          typename RT> RT max (T1 a, T2 b);

// Function call.
max <int,double,double> (4,4.2)

// Function call.
max <int> (4,4.2)

One case may be when you need to specify the return type.

Is there any other situation which requires the argument types to be specified manually?

Edit
Report

1 Answer

2

In general, you need to explicitly specify the types when the compiler can't figure it out on its own. As you mentioned, this often happens when the return type is templatized, since the return type cannot be inferred from the function call.

Template classes have the same problem -- instantiating a std::vector offers no way for the compiler to determine what type your vector is storing, so you need to specify std::vector<int> and so forth.

The type resolution is only performed in the case of function arguments, so it may be easier to view that as the special case; ordinarily, the compiler is unable to guess what type(s) to use.

answered 2011-08-29T05:46:07.030

Your Answer