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

On 02/05/2013 05:06 AM, pilotmm wrote:
> 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.

please state which version of openSUSE you are using…


dd

pilotmm wrote:

> 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.

> 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.

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

Your problem and both your questions seem to be entirely concerned with
gcc and not with openSUSE. So I think you’re likely to get better
answers on a gcc list http://gcc.gnu.org/lists.html

Hi,

If it makes you feel any better this compiles using GCC 4.7.2 on my machine. So if it is GCC it’s not a permanent change.

But the C++11 change from NULL to nullptr is permanent so once you start using the new standard you shouldn’t have this problem anyway.

To check if it is GCC, have you tried to compile it with Clang?

Barry

On Tue, 5 Feb 2013, pilotmm 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:
>
>
> Code:
> --------------------
> #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
>
>
> –
> pilotmm
> ------------------------------------------------------------------------
> pilotmm’s Profile: http://forums.opensuse.org/member.php?userid=29174
> View this thread: http://forums.opensuse.org/showthread.php?t=482978
>
>

Hello All

     My apologies this is not a suse specific thing. However I thought it was relavent since these were errors in the default versions of gcc for the various versions of suse (11.4 gives you gcc v4.5.1 while 12.2 gives you gcc v4.7.1). I thought that there might be a chance that some suse developers are aware of this obscure problem.

     Thanks a lot for all your thoughts though.

     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:

C++ Standard Library Defect Report List
C++ Standard Library Defect Report List

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

Bug 43813 – [DR1234] vector<T*>(3, NULL) fails to compile

  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):

  3. vector<T*> myVect(size,0L)

  4. vector<T*> myVect(size,nullptr) for the C++11 standard

  5. vector<T*> myVect(size,(T*)NULL)

  6. 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

On 02/06/2013 03:56 AM, pilotmm wrote:
> I thought that there might be a chance that
> some suse developers are aware of this obscure problem.

i’m sure the devs are aware of these issues, but there are just a little
over zero of them here in these webforums…

the openSUSE forums are populated by users helping users…

the devs seldom come here looking for problems to fix or users to help
[we keep them in the dungeon with their heads down and fingers
working–no need in tasking them with the 300th question today on how to
change that ‘ugly’ desktop background or ‘horrible’ sound which comes
when . . .]…

if you wish to discuss problems/questions etc with the openSUSE/SUSE
developers one needs to do that via the mail lists, IRC, or
bugzilla…the starting point for all of those is
http://en.opensuse.org/openSUSE:Communication_channels


dd
openSUSE®, the “German Engineered Automobile” of operating systems!