Tested with Ideone.com compiler with C++14 setting.
The WriteLn function presented here is based on one I found on CodeReview.
#include <fstream>
#include <iostream>
#include <utility>
auto WriteLn (std::ostream &os) -> std::ostream&
{
os << "\n" ;
return os ;
}
template <typename T, typename ... Args>
auto WriteLn (std::ostream &os, T first, Args&& ... args) -> std::ostream&
{
os << first ;
WriteLn (os, std::forward <Args> (args)...) ;
return os ;
}
template <typename T1, typename T2>
auto sum (T1 t1, T2 t2) -> decltype (t1 + t2)
{
return t1 + t2 ;
}
template <typename T, typename ... Args>
auto sum (T t, Args ... args) -> decltype (t + sum (args...))
{
return t + sum (args...) ;
}
int main ()
{
int x = 5 ;
WriteLn (std::cout, "Hello. ", "\n", "x = ", x, ".") ;
WriteLn (std::cout, sum (5.32, 1.1, 5)) ;
return 0;
}
No comments:
Post a Comment