vendredi 27 février 2015

trying to understand -Wsign-conversion error

I have the following sample program, which gives me an error:


error: conversion to ‘__gnu_cxx::__normal_iterator<long unsigned int*, std::vector<long unsigned int> >::difference_type {aka long int}’ from ‘std::size_t {aka long unsigned int}’ may change the sign of the result [-Werror=sign-conversion] for (auto iter = v.begin(); iter != v.begin() + pos; ++iter) { ^ cc1plus: all warnings being treated as errors


if I compile it with -Wsign-conversion, i.e.: g++ ./test.cpp -std=c++11 -Wsign-conversion


source:



#include <vector>
#include <iostream>

int main() {
std::vector<std::size_t> v;
for (std::size_t i = 0; i < 10; ++i) {
v.push_back(i);
}

std::size_t pos = 4;

for (auto iter = v.begin(); iter != v.begin() + pos; ++iter) {
std::cout << *iter << " ";
}

return 0;
}


Can you explain why it is an error, and how should I go about dealing with it? Thank you.


Aucun commentaire:

Enregistrer un commentaire