Although I've been coding C++ for many years, I'm relatively new to the standard library (and many other modern C++ features). Going through my code and implementing std::vector
(especially for use in parallelisation), I came across an error I really don't understand. Code below is a MCRE (I'm actually using nested loops for processing a bitmap):
#include <vector>
#include <numeric>
#include <algorithm>
#include <execution>
#include <iostream>
using namespace std;
int main()
{
int32_t h = 12;
cout << "First loop:" << endl;
vector<unsigned int> vecH(static_cast<size_t>(h)); iota(begin(vecH), end(vecH), 0);
// vector<unsigned int> vecH(size_t(h)); iota(begin(vecH), end(vecH), 0); // Gives error!
for_each(execution::par, begin(vecH), end(vecH), [&](int py) {
cout << py << " ";
});
cout << "." << endl;
return 1;
}
The output order is jumbled up, but that's down to parallelisation. What confuses me is why I can't use size_t(h)
in the vecH
constructor!
Of course, I can just use the static_cast
version but, maybe, there is something subtle here I don't understand, which may jump up and bit me on the behind if I don't get to grips with it.
With MSVC (VS-2019, 32- or 64-bit build), I get the following error messages when I use the commented-out line:
StackFlow.cpp(14,48): error C2672: 'begin': no matching overloaded function found
StackFlow.cpp(14,58): error C2893: Failed to specialize function template 'unknown-type std::begin(_Container &)'
StackFlow.cpp(14,58): message : With the following template arguments:
StackFlow.cpp(14,58): message : '_Container=std::vector<unsigned int,std::allocator<_Ty>> (size_t)'
StackFlow.cpp(14,58): error C2784: 'const _Elem *std::begin(std::initializer_list<_Elem>) noexcept': could not deduce template argument for 'std::initializer_list<_Elem>' from 'std::vector<unsigned int,std::allocator<_Ty>> (size_t)'
StackFlow.cpp(14,58): error C2784: with
StackFlow.cpp(14,58): error C2784: [
StackFlow.cpp(14,58): error C2784: _Ty=unsigned int
StackFlow.cpp(14,58): error C2784: ]
EDIT: Can't (yet?) post my own answer, but I should've run the code through clang-cl:
StackFlow.cpp(14,30): warning : parentheses were disambiguated as a function declaration [-Wvexing-parse]
StackFlow.cpp(14,31): message : add a pair of parentheses to declare a variable
StackFlow.cpp(14,48): error : no matching function for call to 'begin'