T shall meet the requirements of CopyConstructible (Table 21)
T u = v;
On 2015–02–01, at 2:44 PM, Vlad from Moscow <vlad....@mail.ru> wrote:I do not see any reason why a type with explicit copy constructor may not be used in the algorithm std::accumulate.
The accumulator of std::accumulate could be directly initialized by init.
Also it seems that the second template argument may not be a reference. This is also a disadvantage.
typename std::remove_reference<T>::type shall meet the requirements of CopyConstructible (Table 21) and CopyAssignable
T shall meet the requirements of CopyConstructible (Table 21) and CopyAssignable
in the algorithm description?
On Sunday, February 1, 2015 at 10:00:48 AM UTC+3, David Krauss wrote:
On 2015–02–01, at 4:49 PM, Vlad from Moscow <vlad....@mail.ru> wrote:But in this case when the second template argument can be specified explicitly like a reference
should there be specified thattypename std::remove_reference<T>::type shall meet the requirements of CopyConstructible (Table 21) and CopyAssignable
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-discussion/.
You have reminded me about std::iota. I forgot to include it in the standard proposal.:)First of all if you want to forward the result from one algorithm to another algorithm then you should take into account that for example my proposal to declare std::iota such a way that it would return the accumulated value was rejected by the C++ Standards Committee without any reason.:)I believe that three algorithms, std::accumulate, std::inner_product, and std::iota, should acccept initializer by reference and accordingly process it. That is I suggest that for example this program#include <iostream>
#include <numeric>
#include <iterator>int main()
{
const size_t N = 10;
int a[N];
int value = 0;
std::iota<decltype( std::begin( a ) ), int &>
( std::begin( a ), std::end( a ), value );
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "value = " << value << std::endl;
return 0;
}would yield result0 1 2 3 4 5 6 7 8 9
value = 10However in any case std::iota shall return value as I proposed independing on whether it is passed by reference.The program above has different output if to compile it with the on-line MS VC++ compiler that is second line of the output isvalue = 0The possibility to have the second template argument as reference enlarges the functionality of algorithms.
#include <iostream>
#include <numeric>
#include <iterator>
#include <functional>
int main()
{
const size_t N = 10;
int a[N];
int value = 0;
std::iota(std::begin(a), std::end(a), std::ref(value));
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
std::cout << "value = " << value << std::endl;
return 0;
}
On Monday, February 23, 2015 at 4:19:24 PM UTC+3, David Rodríguez Ibeas wrote:
This codeint value = 0;std::accumulate( std::begin( a ), std::end( a ), std::ref( value ) );
will not be compiled.
template <class ForwardIterator, class T>
void iota(ForwardIterator first, ForwardIterator last, T value);
1 Requires: T shall be convertible to ForwardIterator’s value type. The expression ++val, where val
has type T, shall be well formed.
#include <iostream>
#include <iterator>
template <typename InputIterator, typename T>
T accumulate( InputIterator first, InputIterator last, T init )
{
decltype( auto ) acc = init;
for ( ; first != last; ++first ) acc = acc + *first;
return init;
}
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int sum = 0;
accumulate<int *, int &>( std::begin( a ), std::end( a ), sum );
std::cout << "sum = " << sum << std::endl;
}
#include <iostream>
#include <iterator>
template <typename InputIterator, typename T>
T accumulate( InputIterator first, InputIterator last, T init )
{
for ( ; first != last; ++first ) init = init + *first;
return init;
}
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int sum = 0;
accumulate<int *, int &>( std::begin( a ), std::end( a ), sum );
std::cout << "sum = " << sum << std::endl;
}
template <typename InputIterator, typename T>
T accumulate( InputIterator first, InputIterator last, T init )
{
decltype( auto ) acc = init;
for ( ; first != last; ++first ) acc = acc + *first;
return acc;
}
There is a typo in the previous post. I meant
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
On Thu, 7 Jun 2018 at 15:29, 'Vlad from Moscow' via ISO C++ Standard - Discussion <std-dis...@isocpp.org> wrote:There is a typo in the previous post. I meantSurely if it's to be fixed, it should be properly fixed?
Uh oh... I'm falling down a rabbit hole...template <typename FirstInputIterator, typename LastInputIterator, typename T, typename BinaryFunction = std::plus<>>constexpr auto accumulate( FirstInputIterator first, LastInputIterator last, T init, BinaryFunction func = BinaryFunction() )noexcept(noexcept(std::declval<BinaryFunction>()(init, *first)))-> decltype(std::declval<BinaryFunction>()(std::declval<std::common_type_t<T, decltype(*first)>>(), *first)){decltype(std::declval<BinaryFunction>()(std::declval<std::common_type_t<T, decltype(*first)>>(), *first)) accum = init;for ( ; first != last; ++first )accum = func(init, *first);return accum;}