Chiel Chiel - 7 months ago 35
C++ Question

Return type deduction in C++14 in assignment

I was wondering whether a return type deduction in an assignment is possible in C++14 in some way. It feels redundant to type the

after the
function name. Thus, in other words, can the compiler use information from the left hand side of the assignment?

#include <iostream>
#include <string>

template<typename T>
auto return_five()
return static_cast<T>(5);

int main()
int five_int = return_five(); // THIS DOES NOT WORK
// int five_int = return_five<int>(); // THIS WORKS

std::cout << "Five_int = " << five_int << std::endl;

return 0;


C++ ain't VBA: the thing on the left hand side of the assignment is not used to deduce the type of the right hand side.

So the compiler requires an explicit type for return_five(). You inform the compiler of the type by writing return_five<int>().