0

I am facing a compilation error with one g++ version (2.9-gnupro-98r2)for LynxOS-178B 2.2.2, whereas the same code will be compiled without complaints with a newer version of g++, e.g. 4.3.3 for VxWorks 653 2.4.0.2.

The following example illustrates the problem:

int function_1(int)
{
  return 4;
}

double function_2(double)
{
  return 2;
}

typedef int (*fp1)(int);
typedef double (*fp2)(double);

struct A
{
  operator fp1()
  { 
    return function_1;                       // conversion function to pointer to function
  }

  operator fp2()
  {
    return function_2;                      // conversion function to pointer to function
  }

}a;

int call_to_class_object_1()
{
  double i = a(3.6);                       // calls function_2 via pointer returned from conversion function
  return i;
}

int call_to_class_object_2()
{
  int i = a(6);                            // calls function_1 via pointer returned from conversion function
  return i;
}

where I am calling double i = a(3.6); and int i = a(6); I am getting error

For 2.9-gnupro-98r2 I am getting:

../../src/Overloading_13_3_1_1_2_Call_to_object_of_class_type.cpp(30) : error: Internal compiler error.
../../src/Overloading_13_3_1_1_2_Call_to_object_of_class_type.cpp(30) : error: Please submit a Problem Report to Lynx Technical Support (support@lynx.com).
make[1]: *** [Overloading_13_3_1_1_2_Call_to_object_of_class_type.o] Error 1

If one compiler version accepts the code it cannot be completely wrong. My guess is that it conforms to the C++ standard, but the older compiler was lacking a proper implementation of the same.

  1. What exactly is the problem?
  2. Is there a portable way of writing that kind of code such that as much compilers as possible accept it?
Alok
  • 163
  • 1
  • 11
Anu
  • 19
  • 4
  • I can't repro because I don't have access to that compiler. Must be something wrong with parsing user-defined conversions as function pointers. What if you defined `operator()` for `struct A` that accepted `int` or `double` and returned the appropriate thing? – AndyG Aug 29 '16 at 20:23
  • I used the example right from the standards document ANSI ISO-IEC 14882-2003_C++ Spec ,section 13.3.1.1.2 Call to object of class type, works for gcc 4.3.3 and does not work for 2.9 – Anu Aug 29 '16 at 22:16

0 Answers0