Home Accept both int and class in variadic templated function?
Reply: 0

Accept both int and class in variadic templated function?

user1306
1#
user1306 Published in July 21, 2018, 1:52 pm

Wrestling templates for this one function is proving to be beyond me.


What I want:

A function that produces the same float type as the one given to it inside any of the following containers:

  • vector<ANY_FLOAT_TYPE>
  • array<ANY_FLOAT_TYPE, N>

(I'm open to having it accept even more containers, of course!)


The Problem:

The problem lies with array<> accepting an integer as its second template argument while vector<> accepts an allocator in that position. How do I write a templated function that can accept both typenames and ints for a single argument? Or is this the wrong way to proceed and write this function?

I could just copy-paste and write the function twice, both for vector<>s and array<>s, but that's no solution...

template <typename FT, typename CONT_T, typename... Ts>
FT float_sum( CONT_T<FT, Ts...> xs ) {

    // WARNING: not an accurate summation algorithm!
    return accumulate( xs.begin(), xs.end(), 0 );

    //////////////
    static_assert( is_floating_point<FT>::value, "sum() only accepts floating point types." );
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301948 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO