public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* (undocumented?) difference between gcc 4.6 and 4.7 (C++ implicit template instantiation)
@ 2012-08-28 17:24 Christoph Groth
  2012-08-28 17:29 ` Jonathan Wakely
  0 siblings, 1 reply; 3+ messages in thread
From: Christoph Groth @ 2012-08-28 17:24 UTC (permalink / raw)
  To: gcc-help

Dear GCC experts,

While developing a Python extension using CPython's C API in C++ I've
encountered a situation where a program compiles with GCC 4.7.1 but does
not compile with 4.6.3.  I have attached a minimal program demonstrating
the problem.  As a workaround, adding a explicit instantiation makes the
program also compile under GCC 4.6.

I've been unable to find a description of this issue in any changelog.
Actually, I wonder whether the behavior of 4.6 or that of 4.7 is the
correct one with regard to the C++ language standard.

Is this a known issue which has been fixed in 4.7, or rather a
regression from 4.6?



// Compile with: g++ test.cc

typedef void *(*Func)(void *);

template <typename T>
class Class {
public:
    static Func hoho;
};

template <typename T>
void *something(Class<T> *self)
{
}

// The following line makes it compile under GCC 4.6
// template void *something(Class<long>*);

template <typename T>
Func Class<T>::hoho = (Func)something<T>;

// Explicit instantiation.  This does instantiate something<long>
// under GCC 4.7 but not under GCC 4.6.
template class Class<long>;

int main()
{
    Class<long>::hoho(0);
}

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: (undocumented?) difference between gcc 4.6 and 4.7 (C++ implicit template instantiation)
  2012-08-28 17:24 (undocumented?) difference between gcc 4.6 and 4.7 (C++ implicit template instantiation) Christoph Groth
@ 2012-08-28 17:29 ` Jonathan Wakely
  2012-08-28 20:32   ` Christoph Groth
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Wakely @ 2012-08-28 17:29 UTC (permalink / raw)
  To: Christoph Groth; +Cc: gcc-help

On 28 August 2012 16:32, Christoph Groth wrote:
> Dear GCC experts,
>
> I've been unable to find a description of this issue in any changelog.
> Actually, I wonder whether the behavior of 4.6 or that of 4.7 is the
> correct one with regard to the C++ language standard.
>
> Is this a known issue which has been fixed in 4.7, or rather a
> regression from 4.6?

The change in behaviour might be due to the patch for
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48883

But I think the program has undefined behaviour according to
[expr.reinterpret.cast]/6, because it calls a function through a
pointer to a different function type.  Specifically, something<long>
which has type void*(Class<long>*) is called through a pointer of type
void*(*)(void*).

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: (undocumented?) difference between gcc 4.6 and 4.7 (C++ implicit template instantiation)
  2012-08-28 17:29 ` Jonathan Wakely
@ 2012-08-28 20:32   ` Christoph Groth
  0 siblings, 0 replies; 3+ messages in thread
From: Christoph Groth @ 2012-08-28 20:32 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc-help

Thank you, Jonathan, for your quick reply.

Jonathan Wakely <jwakely.gcc@gmail.com> writes:

> On 28 August 2012 16:32, Christoph Groth wrote:
>> I've been unable to find a description of this issue in any changelog.
>> Actually, I wonder whether the behavior of 4.6 or that of 4.7 is the
>> correct one with regard to the C++ language standard.
>>
>> Is this a known issue which has been fixed in 4.7, or rather a
>> regression from 4.6?
>
> The change in behaviour might be due to the patch for
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48883
>
> But I think the program has undefined behaviour according to
> [expr.reinterpret.cast]/6, because it calls a function through a
> pointer to a different function type.  Specifically, something<long>
> which has type void*(Class<long>*) is called through a pointer of type
> void*(*)(void*).

Unfortunately, this is the official way of programming Python extensions
in C/C++. [1]  The void* of my example stands for PyObject*.

I'm not sure whether the behavior is indeed undefined when
something<long> will be called through a pointer of type void*(*)(void*)
IF the void* argument always points to a Class<long> object.

So, I guess, I either have to keep all the explicit instantiations in my
program (many) or make all these template functions static methods of a
class which can then be instantiated explicitly with a single statement.

[1] http://docs.python.org/py3k/extending/newtypes.html

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-08-28 18:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-28 17:24 (undocumented?) difference between gcc 4.6 and 4.7 (C++ implicit template instantiation) Christoph Groth
2012-08-28 17:29 ` Jonathan Wakely
2012-08-28 20:32   ` Christoph Groth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).