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

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.

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.

First Question: 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 know there is an easy fix. However I have a large amount of code that has 8000+ instances of the work 'NULL'. I do not want to change all those instances to something like '(type*)0' just to find out later that this is a compiler error.

Second Question: Is there a simple work around so I do not have to change all those instances of NULL?

I know that I can stick with the 4.5.1 version of gcc. However if this is a permanent change in the compiler then I want to make the code work in future versions of g++.

Thanks
Mike
Last edited on
It was technically a bug on the C++ standard (http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#438 and http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#1234), where it was fixed in time for C++11.

In GCC, this is bug 43813: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43813 , fixed in 4.8.0

(double*)NULL would work.
Last edited on
Hi Cubbi

Thanks a tonne for responding to this.

I looked into this through a couple forums and have come up with many resolutions and explanations. For the benefit of everyone else I am posting the results here. So yes this was an error in the standard:

http://open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#438
http://open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#1234

This was addressed in the C++11 standard and then reported as a bug for GCC and fixed in the 4.8.0 version:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43813

Some details on the bug is that it resolves to the following constructor: vector(iterator,iterator) as appose to vector(size_t,value_t). This error has been a problem since versions earlier than 4.5.1. It only happens on 32 bit versions of gcc.

Some valid one line work-a-arounds:

1) redefine NULL as 'nullptr' for the C++11 standard
2) redefine NULL as '0L' as it is in the 64bit version

Alternate fixes for each instance of vector<T*> myVect(size,NULL):

1) vector<T*> myVect(size,0L)
2) vector<T*> myVect(size,nullptr) for the C++11 standard
3) vector<T*> myVect(size,(T*)NULL)
4) vector<T*> myVect(size,static_cast<T*>(NULL))

I need to check to see if this works on my colleagues computer. My version of GCC works.

Take Care
Mike
Last edited on
Topic archived. No new replies allowed.