Uhmmm...
1) the following code isn't legal C++ code, at the moment; maybe in future (C++20?) but not until C++17
auto sum(const auto& x1, const auto& x2)
{
return x1 + x2;
}
2) it's a valid code (but only from C++14) your template code
template<class X1, class X2> auto sum1(const X1& x1, const X2& x2)
{
return x1 + x2;
}
3) a valid alternative is a generic-lambda (also from C++14)
[](auto const & x1, auto const & x2){ return x1+x2; }
4) in C++11 you can't use simply auto
for return type but you have to explicit it with a trailing return type; by example, with decltype()
in the following code
template<class X1, class X2>
auto sum1 (X1 const & x1, X2 const & x2)
-> decltype( x1+x2 )
{ return x1 + x2; }
or also without auto
template<class X1, class X2>
decltype( std::declval<X1 const>() + std::declval<X2 const>() )
sum1 (X1 const & x1, X2 const & x2)
{ return x1 + x2; }
5) a generic-lambda can (roughly) replace a function template but, starting from C++20, a lambda function could be (probably) a template one itself with a syntax as follows
[]<typename X1, typename X2>(X1 const & x1, X2 const &x2){ return x1+x2) }