0

My problem seems somewhat similar to Conversion from null-integer to pointer in comma list

Here is a minimal example

#include <utility>

struct Object {
  double foo;
};

std::pair<Object*,int> test_function() {
  typedef std::pair<Object*,int>  pair_t;
  return pair_t(NULL, 2);
}
// test.cc

With gcc 4.4.7, g++ -std=c++0x -c test.cc -o test.o fails with

/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_pair.h:90: error: invalid conversion from \u2018long int\u2019 to \u2018Object*

It does compile if I omit -std=c++0x. Also, if I use newer versions of gcc, the compilation is fine. Is there anything wrong with the code? Should one expect such differences in gcc versions?

A later edit: I can also return pair_t((Object*)NULL, 2)

Community
  • 1
  • 1
bernie
  • 546
  • 3
  • 13
  • 2
    Do you heve a specific reason to use `NULL` instead of `nullptr` in post c++11 code? Also, gcc 4.4.7 is *extremely* old, I suggest you upgrade your compiler since that code works flawlessly on a modern compiler: http://coliru.stacked-crooked.com/a/a01d03ae16e6c3bf – Fatih BAKIR Aug 03 '16 at 15:58
  • It may be a bug. FWIW if you are using C++11 you should use `nullptr`. – NathanOliver Aug 03 '16 at 15:58
  • I admit my ignorance of `nullptr`. The code was not originally written for C++11, but I have changed another part of it to include a library that needs C++11. – bernie Aug 03 '16 at 16:03
  • Although gcc 4.6.0+ is needed for it http://stackoverflow.com/questions/3756473/what-header-file-needs-to-be-included-for-using-nullptr-in-g – bernie Aug 03 '16 at 16:08
  • @Fatih BAKIR, I run into other issues with this code with newer gcc versions. I am not the author. – bernie Aug 03 '16 at 16:16

1 Answers1

2

I believe the answer to your question is here:

C++ can't initialize a pointer in a pair to NULL

The NULL is represented as a long by gcc and it cannot be converted to type Object* without the explicit cast.

Community
  • 1
  • 1
Richard
  • 54
  • 5