Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Weird difference between comeau and gcc/clang

Reply
Thread Tools

Weird difference between comeau and gcc/clang

 
 
Johannes Schaub (litb)
Guest
Posts: n/a
 
      10-13-2010
Both gcc and clang do not instantiate the struct definition. But comeau does

template <typename T>
void f()
{
struct haha {
void g() { T t; }
};
}

int main() {
f<void>();
}


And comeau therefor says "T t;" is invalid because t is void. I always
thought that local structs are immediately instantiated along with their
function. But clang and GCC don't do so!

Is GCC/Clang or Comeau correct? I deeply believe GCC and Clang are wrong on
this!
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      10-14-2010
On Oct 13, 8:59 pm, "Johannes Schaub (litb)" <(E-Mail Removed)>
wrote:
> Both gcc and clang do not instantiate the struct definition.
> But comeau does


> template <typename T>
> void f()
> {
> struct haha {
> void g() { T t; }
> };
> }


> int main() {
> f<void>();
> }


> And comeau therefor says "T t;" is invalid because t is void.
> I always thought that local structs are immediately
> instantiated along with their function. But clang and GCC
> don't do so!


> Is GCC/Clang or Comeau correct? I deeply believe GCC and Clang
> are wrong on this!


Typically, if compilers differ, Comeau is correct. In this
case, however, I don't think so. Local structs are
instantiated, but member functions are only instantiated if they
are used, and your use of T is in the member function.

--
James Kanze
 
Reply With Quote
 
 
 
 
Gennaro Prota
Guest
Posts: n/a
 
      10-14-2010
On 14/10/2010 10.47, James Kanze wrote:
> On Oct 13, 8:59 pm, "Johannes Schaub (litb)" <(E-Mail Removed)>
> wrote:
>> Both gcc and clang do not instantiate the struct definition.
>> But comeau does

>
>> template <typename T>
>> void f()
>> {
>> struct haha {
>> void g() { T t; }
>> };
>> }

>
>> int main() {
>> f<void>();
>> }

>
>> And comeau therefor says "T t;" is invalid because t is void.
>> I always thought that local structs are immediately
>> instantiated along with their function. But clang and GCC
>> don't do so!

>
>> Is GCC/Clang or Comeau correct? I deeply believe GCC and Clang
>> are wrong on this!

>
> Typically, if compilers differ, Comeau is correct. In this
> case, however, I don't think so. Local structs are
> instantiated, but member functions are only instantiated if they
> are used, and your use of T is in the member function.


But neither haha nor haha::g are templates. Why do you expect
the behavior you describe?

--
Gennaro Prota | <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
 
Reply With Quote
 
Johannes Schaub (litb)
Guest
Posts: n/a
 
      10-14-2010
James Kanze wrote:

> On Oct 13, 8:59 pm, "Johannes Schaub (litb)" <(E-Mail Removed)>
> wrote:
>> Both gcc and clang do not instantiate the struct definition.
>> But comeau does

>
>> template <typename T>
>> void f()
>> {
>> struct haha {
>> void g() { T t; }
>> };
>> }

>
>> int main() {
>> f<void>();
>> }

>
>> And comeau therefor says "T t;" is invalid because t is void.
>> I always thought that local structs are immediately
>> instantiated along with their function. But clang and GCC
>> don't do so!

>
>> Is GCC/Clang or Comeau correct? I deeply believe GCC and Clang
>> are wrong on this!

>
> Typically, if compilers differ, Comeau is correct. In this
> case, however, I don't think so. Local structs are
> instantiated, but member functions are only instantiated if they
> are used, and your use of T is in the member function.
>


I was not able to get a clear opinion about this. I find it difficult find a
clear statement about it in the IS.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-14-2010
On 10/15/10 09:17 AM, Johannes Schaub (litb) wrote:
> James Kanze wrote:
>
>> On Oct 13, 8:59 pm, "Johannes Schaub (litb)"<(E-Mail Removed)>
>> wrote:
>>> Both gcc and clang do not instantiate the struct definition.
>>> But comeau does

>>
>>> template<typename T>
>>> void f()
>>> {
>>> struct haha {
>>> void g() { T t; }
>>> };
>>> }

>>
>>> int main() {
>>> f<void>();
>>> }

>>
>>> And comeau therefor says "T t;" is invalid because t is void.
>>> I always thought that local structs are immediately
>>> instantiated along with their function. But clang and GCC
>>> don't do so!

>>
>>> Is GCC/Clang or Comeau correct? I deeply believe GCC and Clang
>>> are wrong on this!

>>
>> Typically, if compilers differ, Comeau is correct. In this
>> case, however, I don't think so. Local structs are
>> instantiated, but member functions are only instantiated if they
>> are used, and your use of T is in the member function.
>>

>
> I was not able to get a clear opinion about this. I find it difficult find a
> clear statement about it in the IS.


I thought James' reply was clear and unambiguous. Class template member
functions are only instantiated if they are used. This feature is often
exploited to get different behaviour depending on the template argument.

Or is the confusion whether a local class in a function template a class
template?

--
Ian Collins
 
Reply With Quote
 
Johannes Schaub (litb)
Guest
Posts: n/a
 
      10-14-2010
Ian Collins wrote:

> On 10/15/10 09:17 AM, Johannes Schaub (litb) wrote:
>> James Kanze wrote:
>>
>>> On Oct 13, 8:59 pm, "Johannes Schaub (litb)"<(E-Mail Removed)>
>>> wrote:
>>>> Both gcc and clang do not instantiate the struct definition.
>>>> But comeau does
>>>
>>>> template<typename T>
>>>> void f()
>>>> {
>>>> struct haha {
>>>> void g() { T t; }
>>>> };
>>>> }
>>>
>>>> int main() {
>>>> f<void>();
>>>> }
>>>
>>>> And comeau therefor says "T t;" is invalid because t is void.
>>>> I always thought that local structs are immediately
>>>> instantiated along with their function. But clang and GCC
>>>> don't do so!
>>>
>>>> Is GCC/Clang or Comeau correct? I deeply believe GCC and Clang
>>>> are wrong on this!
>>>
>>> Typically, if compilers differ, Comeau is correct. In this
>>> case, however, I don't think so. Local structs are
>>> instantiated, but member functions are only instantiated if they
>>> are used, and your use of T is in the member function.
>>>

>>
>> I was not able to get a clear opinion about this. I find it difficult
>> find a clear statement about it in the IS.

>
> I thought James' reply was clear and unambiguous. Class template member
> functions are only instantiated if they are used. This feature is often
> exploited to get different behaviour depending on the template argument.
>
> Or is the confusion whether a local class in a function template a class
> template?
>


I am looking for a statement in the IS about it. I haven't been successful
yet. I'm aware about the principle you mention for class template member
functions. Is there a statement about local classes too?

In particular, is an instantiated local class of a function template
specialization a class template specialization? In the list of 14.7/2, ("A
.... is called an instantiated ..."), I do not find local classes.

Ultimately, what paragraphs will the comeau/EDG developers take, and what
paragraphs will GCC and Clang take?
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-14-2010
On 10/15/10 07:24 AM, Gennaro Prota wrote:
> On 14/10/2010 10.47, James Kanze wrote:
>> On Oct 13, 8:59 pm, "Johannes Schaub (litb)"<(E-Mail Removed)>
>> wrote:
>>> Both gcc and clang do not instantiate the struct definition.
>>> But comeau does

>>
>>> template<typename T>
>>> void f()
>>> {
>>> struct haha {
>>> void g() { T t; }
>>> };
>>> }

>>
>>> int main() {
>>> f<void>();
>>> }

>>
>>> And comeau therefor says "T t;" is invalid because t is void.
>>> I always thought that local structs are immediately
>>> instantiated along with their function. But clang and GCC
>>> don't do so!

>>
>>> Is GCC/Clang or Comeau correct? I deeply believe GCC and Clang
>>> are wrong on this!

>>
>> Typically, if compilers differ, Comeau is correct. In this
>> case, however, I don't think so. Local structs are
>> instantiated, but member functions are only instantiated if they
>> are used, and your use of T is in the member function.

>
> But neither haha nor haha::g are templates. Why do you expect
> the behavior you describe?


The question appears to be is a local class in a function template a
class template?

Perhaps comp.std.c++ is the best place to post this question?

--
Ian Collins
 
Reply With Quote
 
Gil
Guest
Posts: n/a
 
      10-15-2010
On Oct 13, 3:59*pm, "Johannes Schaub (litb)" <(E-Mail Removed)>
wrote:
> Both gcc and clang do not instantiate the struct definition. But comeau does
>
> template <typename T>
> void f()
> {
> * struct haha {
> * * void g() { T t; }
> * };
>
> }
>
> int main() {
> * f<void>();
>
> }
>
> And comeau therefor says "T t;" is invalid because t is void. I always
> thought that local structs are immediately instantiated along with their
> function. But clang and GCC don't do so!
>
> Is GCC/Clang or Comeau correct? I deeply believe GCC and Clang are wrong on
> this!


if Comeau diagnostics an error on your code then Comeau is right.
local classes cannot be class templates; template names have
linkage(14.4), local classes don't(9.8.1).

for zealots' delight maybe an explicit wording should be added to 9.8,
something like

9.8.5 A local class shall not be a template.

?

--
gil
 
Reply With Quote
 
Steve Pope
Guest
Posts: n/a
 
      10-15-2010
Gil <(E-Mail Removed)> wrote:

>On Oct 13, 3:59*pm, "Johannes Schaub (litb)" <(E-Mail Removed)>
>wrote:
>> Both gcc and clang do not instantiate the struct definition. But comeau does
>>
>> template <typename T>
>> void f()
>> {
>> * struct haha {
>> * * void g() { T t; }
>> * };
>>
>> }
>>
>> int main() {
>> * f<void>();
>>
>> }
>>
>> And comeau therefor says "T t;" is invalid because t is void. I always
>> thought that local structs are immediately instantiated along with their
>> function. But clang and GCC don't do so!
>>
>> Is GCC/Clang or Comeau correct? I deeply believe GCC and Clang are wrong on
>> this!

>
>if Comeau diagnostics an error on your code then Comeau is right.
>local classes cannot be class templates; template names have
>linkage(14.4), local classes don't(9.8.1).


Does GCC still not issue a warning using -pedantic?


Steve
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      10-15-2010
Gil <(E-Mail Removed)> wrote:
> On Oct 13, 3:59*pm, "Johannes Schaub (litb)" <(E-Mail Removed)>
> wrote:
>> Both gcc and clang do not instantiate the struct definition. But comeau does
>>
>> template <typename T>
>> void f()
>> {
>> * struct haha {
>> * * void g() { T t; }
>> * };
>>
>> }
>>
>> int main() {
>> * f<void>();
>>
>> }
>>
>> And comeau therefor says "T t;" is invalid because t is void. I always
>> thought that local structs are immediately instantiated along with their
>> function. But clang and GCC don't do so!
>>
>> Is GCC/Clang or Comeau correct? I deeply believe GCC and Clang are wrong on
>> this!

>
> if Comeau diagnostics an error on your code then Comeau is right.
> local classes cannot be class templates; template names have
> linkage(14.4), local classes don't(9.8.1).


If Comeau is always right, and local classes can indeed not be
templates, why doesn't Comeau say so, rather then complaining about
a type definition inside a member function of such class?

Could it be that Comeau happens to refuse to compile this example
by pure chance, rather than explicitly having a rule against local
template classes?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
friends, templates and comeau, gcc werasm C++ 4 11-30-2009 05:55 PM
Is a bug with Comeau or I miss something as Comeau says? Rasmus Johansen C++ 4 10-18-2007 04:43 PM
Comeau and G++ disagree. Compiler bug? Alan Woodland C++ 10 08-13-2007 02:43 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
STLPort 4.5.3 compiled with Comeau 4.3.0.1 on Windows? Icosahedron C++ 4 08-18-2003 04:47 PM



Advertisments