C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

aCC vs g++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Michael Krause
Guest





PostPosted: Thu Feb 26, 2004 9:43 am    Post subject: aCC vs g++ Reply with quote




Hello group,

I was wondering, is there anything in this short snippet of code that
violates some C++ standard?

-- cut here --

static void
f (double *z)
{
}


template <class T>
static void g (T *y)
{
f(y);
}

int
main (int argc, char **argv)
{
double x = 0.0;

g(&x);

return 0;
}

-- cut here --

Reason I am asking is, g++ 3.3.2 compiles fine, whereas HP's aCC
B3910B A.03.37 and B3910B A.03.31 produce this error message:

Error 328: "msng.c", line 11 # Function 'f' has not been defined yet; cannot call.
f(y);
^
Error 556: "msng.c", line 19 # Unable to generate specialization "void g<double>(double *)" due to errors during generation.
g(&x);
^^^^^
Error 556: "msng.c", line 19 # Unable to generate specialization "void g<double>(double *)" due to errors during generation.
g(&x);
^^^^^

I am a bit reluctant to say this is a compiler bug, because I've been
using templates for only two days, so maybe I'm just being stupid :-)

Any help on this would be very much appreciated!

bye,
--
/* michael */
Back to top
John Carson
Guest





PostPosted: Thu Feb 26, 2004 12:11 pm    Post subject: Re: aCC vs g++ Reply with quote



"Michael Krause" <m.krause (AT) tu-harburg (DOT) de> wrote

Quote:
Hello group,

I was wondering, is there anything in this short snippet of code that
violates some C++ standard?

-- cut here --

static void
f (double *z)
{
}


template <class T
static void g (T *y)
{
f(y);
}

int
main (int argc, char **argv)
{
double x = 0.0;

g(&x);

return 0;
}

-- cut here --

Reason I am asking is, g++ 3.3.2 compiles fine, whereas HP's aCC
B3910B A.03.37 and B3910B A.03.31 produce this error message:

Error 328: "msng.c", line 11 # Function 'f' has not been defined yet;
cannot call. f(y);
^
Error 556: "msng.c", line 19 # Unable to generate specialization
"void g g(&x); ^^^^^
Error 556: "msng.c", line 19 # Unable to generate specialization
"void g<double>(double *)" due to errors during generation.
g(&x); ^^^^^

I am a bit reluctant to say this is a compiler bug, because I've been
using templates for only two days, so maybe I'm just being stupid :-)

Any help on this would be very much appreciated!

bye,


VC++ 7.1 accepts it but Comeau rejects it. I don't know why, but if you
remove the static keyword from the definition of f, then Comeau accepts it
(along with VC++ 7.1).


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


Back to top
tom_usenet
Guest





PostPosted: Thu Feb 26, 2004 12:49 pm    Post subject: Re: aCC vs g++ Reply with quote



On Thu, 26 Feb 2004 10:43:34 +0100, Michael Krause
<m.krause (AT) tu-harburg (DOT) de> wrote:

Quote:

Hello group,

I was wondering, is there anything in this short snippet of code that
violates some C++ standard?

-- cut here --

static void
f (double *z)
{
}


template <class T
static void g (T *y)
{
f(y);

The f called above is a dependent name, since its meaning depends on
T. This means it is looked up at the point of instantiation.

Quote:
}

int
main (int argc, char **argv)
{
double x = 0.0;

g(&x);

return 0;
}

Point of instantation of g main, where g<double> is used).

f is looked up here, but only external declarations are checked, so
your static f isn't found. See 14.6.4.2 of the standard for details.

Quote:
I am a bit reluctant to say this is a compiler bug, because I've been
using templates for only two days, so maybe I'm just being stupid Smile

This highlights why "static" in the context you have used it above is
deprecated in the C++ standard. Use anonymous namespaces instead:

namespace
{
void f(double *z)
{
}

template <class T>
void g(T *y)
{
f(y);
}
}

int main(int argc, char **argv)
{
double x = 0.0;
g(&x);
return 0;
}

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Back to top
Michael Krause
Guest





PostPosted: Thu Feb 26, 2004 1:28 pm    Post subject: Re: aCC vs g++ Reply with quote

On Thu, 26 Feb 2004, tom_usenet wrote:

Quote:
static void
f (double *z)
{
}


template <class T
static void g (T *y)
{
f(y);

The f called above is a dependent name, since its meaning depends on
T. This means it is looked up at the point of instantiation.

}

int
main (int argc, char **argv)
{
double x = 0.0;

g(&x);

return 0;
}

Point of instantation of g main, where g<double> is used).

f is looked up here, but only external declarations are checked, so
your static f isn't found. See 14.6.4.2 of the standard for details.

I am a bit reluctant to say this is a compiler bug, because I've been
using templates for only two days, so maybe I'm just being stupid :-)

This highlights why "static" in the context you have used it above is
deprecated in the C++ standard. Use anonymous namespaces instead: [...]

Wow, it works. Just leaving out the 'static' in the definition of f
fixes my problem, too. Seems like I was thinking too much "C" here,
please excuse :)

I suppose there is a good reason why only external declarations for f
are checked during instantiation of g<double>?

Thanks again,
--
/* michael */

Back to top
tom_usenet
Guest





PostPosted: Thu Feb 26, 2004 3:05 pm    Post subject: Re: aCC vs g++ Reply with quote

On Thu, 26 Feb 2004 14:28:16 +0100, Michael Krause
<m.krause (AT) tu-harburg (DOT) de> wrote:

Quote:
Wow, it works. Just leaving out the 'static' in the definition of f
fixes my problem, too. Seems like I was thinking too much "C" here,
please excuse :)

I suppose there is a good reason why only external declarations for f
are checked during instantiation of g<double>?

I suspect it has something to do with avoiding violations of the ODR
in template instantiations, export, etc. Implicit template
instantiations from different translation units have to be merged (or
only one generated in the first place), and names with internal
linkage might cause some problems with this.

Certainly the rule could have been avoided, but deprecating static
works just as well, since there's no good reason to use it in C++.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.