This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Cannot convert int (aka NULL) to std::vector<double*>::value_type


On 5 February 2013 04:08,  <mmcwilli@uvic.ca> wrote:
> Hello All
>
> I have a compilation error in a very large program. We have managed to
> isolate the problem with the following test problem:
>
> #include <vector>
> #include <iostream>
>
> using namespace std;
>
> int main()
> {
> double* myPointer=NULL; // no error on this line
> vector<double*> myVect(1,NULL); // error on this line
> return 0;
> }
>
> Compiling this spits out the error:
>
> Cannot convert 'int' to 'std::vector<double*>::value_type {aka double*}'
>
> When I compile this on GNU gcc compiler version 4.5.1 I do not get this
> error. When I compile it on GNU gcc compiler version 4.7.1 I get the
> error.

Really?  I get no error with 4.5.1 or 4.7.2 in 64-bit mode, and an
error with both in 32-bit mode.  Maybe you're using a 32-bit 4.7.1 and
64-bit 4.5.1?

> Note that this error occurs on a colleagues computer that I do not have
> access to so I cannot give the exact source code and the exact error and
> all the other details.

That's never very helpful, could your colleague at least tell you the
precise compiler versions and command line used?

> Since the first reference to NULL works then we can conclude that NULL is
> defined and it can be used to initialize pointers. However when NULL is
> passed as an initialization to a std::vector container it is no longer
> type compatible.
>
> Is this a bug in the 4.7.1 compiler or did the compiler change to conform
> to some standard that does not allow vector's to initialized to NULL.

I don't think it changed in quite some time. It's a very well known
problem, see http://open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#438,
but the implementation is supposed to ensure it works.  I can't
remember if we have an open bug about it still.

There are numerous workarounds, including those already given in the
thread (casting NULL to a pointer type or passing a value of type
double*) as well as:

std::vector<double> myVect(1);
std::vector<double> myVect(size_t(1), NULL);
std::vector<double> myVect(1ul, NULL);


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]