Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C++14] So I had some fun with templates and averages...
#2
At first, I didn't really know what half of the code was doing (rarely did I even get past C++98, let alone 14), so I had to do my reading before I comment.

The idea of having functions be as flexible as that is rather brilliant, and I can see myself adopting it :P.
The compiler did throw 2 errors though (about not able to figure out the return types for "auto" of the average and size functions). Using "-> decltype(returntype)" after each function's declaration fixed the problem.
    CPP-Code:
template <typename Cont>
//requires std::Container<Cont>
inline auto size(const Cont &cont) -> decltype(cont.size())
{
    return cont.size();
}
 
template <typename Cont>
//requires std::Container<Cont>
auto average(const Cont &cont) -> decltype(*begin(cont))
{
    auto Sum = decltype(*begin(cont))(0);//deduce the return type, ugly hack, ISO committee please...
 
    for(auto &e:cont) Sum += e;
 
    return Sum/size(cont);
}


I can't understand what exactly did you do there for the new size() function.

(I find that hack to be kind of neat btw)
[Image: signature.png]
A-Engine: A new beat em up game engine inspired by LF2. Coming soon

A-Engine Dev Blog - Update #8: Timeout

Reply
Thanks given by:


Messages In This Thread
RE: [C++14] So I had some fun with templates and averages... - by A-Man - 01-28-2015, 08:26 AM



Users browsing this thread: 1 Guest(s)