c++/8041: Array to pointer conversion in cast expression

René Møller Fonseca fonseca@mip.sdu.dk
Fri Feb 14 18:58:00 GMT 2003


http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8041

This "implicit casting" problem seems to be related (checked with GCC 3.2 under GNU/Linux and Solaris).

#include <iostream>

using namespace std;

class MyClass {
public:

  template<size_t SIZE>
  MyClass(const int (&x)[SIZE]) throw() {
    cout << "int[" << SIZE << "]" << endl;
  }  

  template<size_t SIZE>
  MyClass(long (&x)[SIZE]) throw() {
    cout << "long[" << SIZE << "]" << endl;
  }
};

MyClass myIntFunction() throw() {
  static const int INT_ARRAY[999] = {}; // const
  return INT_ARRAY; // this fails to compile
  // GCC ERROR: conversion from `const int*' to non-scalar type `MyClass'
}

MyClass myLongFunction() throw() {
  static long LONG_ARRAY[1234] = {}; // mutable
  return LONG_ARRAY; // this fails to compile
  // GCC ERROR: conversion from `long int*' to non-scalar type `MyClass'
}

int main() {
  static const int INT_ARRAY[123] = {};

  MyClass a(INT_ARRAY); // this works
  MyClass b = INT_ARRAY; // this works
  
  myIntFunction();
  myLongFunction();
  return 0;
}



More information about the Gcc-bugs mailing list