[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Template technicality - What does the standard say?

Stephen Horne

10/13/2008 3:37:00 AM


I've been using Visual C++ 2003 for some time, and recently started
working on making my code compile in GCC and MinGW. I hit on lots of
unexpected problems which boil down to the same template issue. A
noddy mixin layer example should illustrate the issue...

class Base
{
protected:
int m_Field;

public:
Base () : m_Field (0) {}
};

template<class T> class Derived : public T
{
public:
void Inc ()
{
T::m_Field++; // GCC needs the "T::" here - VC++ doesn't
}
};

In the above, the idea is to use Derived<Base>, but GCC (3.4.5 and
4.1.2) gives error messages even if you never instantiate the
template.

There are of course examples of similar templates in Stroustrup, but
because they are incomplete examples, I can't find one that shows an
inherited member being referenced from within the template class.

In non-template classes, scope resolution is only needed to access
inherited members if there is an ambiguity. There is no ambiguity in
the above - the only place m_Field can come from is the inherited
class Base via the template parameter T. Following that general
principle, I don't see any reason why scope resolution should be
needed.

What does the standard say about this - should the scope resolution
operator be required? If it is required, what's the rationale? If
there's something in Stroustrup about this, can you point me to the
right section?

40 Answers

Michael DOUBEZ

10/13/2008 7:12:00 AM

0

Stephen Horne a écrit :
> I've been using Visual C++ 2003 for some time, and recently started
> working on making my code compile in GCC and MinGW. I hit on lots of
> unexpected problems which boil down to the same template issue. A
> noddy mixin layer example should illustrate the issue...
>
> class Base
> {
> protected:
> int m_Field;
>
> public:
> Base () : m_Field (0) {}
> };
>
> template<class T> class Derived : public T
> {
> public:
> void Inc ()
> {
> T::m_Field++; // GCC needs the "T::" here - VC++ doesn't
> }
> };
>
> In the above, the idea is to use Derived<Base>, but GCC (3.4.5 and
> 4.1.2) gives error messages even if you never instantiate the
> template.
[snip]
> What does the standard say about this - should the scope resolution
> operator be required? If it is required, what's the rationale? If
> there's something in Stroustrup about this, can you point me to the
> right section?
>

Look up template-dependant name:
http://www.parashift.com/c++-faq-lite/templates.html...

--
Michael

Stephen Horne

10/13/2008 8:31:00 AM

0

On Mon, 13 Oct 2008 09:12:09 +0200, Michael DOUBEZ
<michael.doubez@free.fr> wrote:

>Look up template-dependant name:
>http://www.parashift.com/c++-faq-lite/templates.html...

Thanks.

Just read 35.18, 35.19 and 35.20. Now I *know* that the lunatics are
running the asylum. Different scoping/lookup rules depending on
whether something is "dependent" or "non-dependent" - pathetic. Next
it'll be phases of the moon.

Anyway, now that's done, I just have to figure out why GCC won't let
me use offsetof in a constant expression - ie in exactly the kind of
place you're always going to use offsetof. Where the last lot was an
"oh well, a few hours hunting and fixing" thing, this is a real
killer.

I have huge amounts of data-driven code that *needs* to use offsetof
in constant expressions to initialise static data. I also have code
generators that generate code using offsetof in constant expressions.
It'd be easier to develop my own offshoot of C++ than to never use
offsetof in constant expressions. It's the kind of thing people have
been doing in C and C++ since dinosaurs roamed the Earth, and frankly,
if someone has decided to ban it, that someone is a moron.

Sigh.

Gianni Mariani

10/13/2008 12:39:00 PM

0

Stephen Horne wrote:
> On Mon, 13 Oct 2008 09:12:09 +0200, Michael DOUBEZ
> <michael.doubez@free.fr> wrote:
>
>> Look up template-dependant name:
>> http://www.parashift.com/c++-faq-lite/templates.html...
>
> Thanks.
>
> Just read 35.18, 35.19 and 35.20. Now I *know* that the lunatics are
> running the asylum. Different scoping/lookup rules depending on
> whether something is "dependent" or "non-dependent" - pathetic. Next
> it'll be phases of the moon.
>
> Anyway, now that's done, I just have to figure out why GCC won't let
> me use offsetof in a constant expression - ie in exactly the kind of
> place you're always going to use offsetof. Where the last lot was an
> "oh well, a few hours hunting and fixing" thing, this is a real
> killer.

GCC does not let you use it as a constant because the standard says so.
SUCCS, I know, just is. You may get some luck out of using pointer to
member types.

>
> I have huge amounts of data-driven code that *needs* to use offsetof
> in constant expressions to initialise static data. I also have code
> generators that generate code using offsetof in constant expressions.
> It'd be easier to develop my own offshoot of C++ than to never use
> offsetof in constant expressions. It's the kind of thing people have
> been doing in C and C++ since dinosaurs roamed the Earth, and frankly,
> if someone has decided to ban it, that someone is a moron.

You can probably refactor to use pointer to member.

If you have a code example, post it in a new thread and lets see if we
can give you a few suggestions.

Michael DOUBEZ

10/13/2008 12:48:00 PM

0

Stephen Horne a écrit :
[snip]
> Anyway, now that's done, I just have to figure out why GCC won't let
> me use offsetof in a constant expression - ie in exactly the kind of
> place you're always going to use offsetof.

You should be able to use it in a constant expression.
Which version of gcc are you using ?

--
Michael

Erik Wikström

10/13/2008 4:09:00 PM

0

On 2008-10-13 14:38, Gianni Mariani wrote:
> Stephen Horne wrote:
>> On Mon, 13 Oct 2008 09:12:09 +0200, Michael DOUBEZ
>> <michael.doubez@free.fr> wrote:
>>
>>> Look up template-dependant name:
>>> http://www.parashift.com/c++-faq-lite/templates.html...
>>
>> Thanks.
>>
>> Just read 35.18, 35.19 and 35.20. Now I *know* that the lunatics are
>> running the asylum. Different scoping/lookup rules depending on
>> whether something is "dependent" or "non-dependent" - pathetic. Next
>> it'll be phases of the moon.
>>
>> Anyway, now that's done, I just have to figure out why GCC won't let
>> me use offsetof in a constant expression - ie in exactly the kind of
>> place you're always going to use offsetof. Where the last lot was an
>> "oh well, a few hours hunting and fixing" thing, this is a real
>> killer.
>
> GCC does not let you use it as a constant because the standard says so.
> SUCCS, I know, just is. You may get some luck out of using pointer to
> member types.

What is it that I'm missing, offsetof is a macro, it should be expanded
before the compiler can even begin worrying about whether an expression
is constant or not.

--
Erik Wikström

James Kanze

10/13/2008 5:09:00 PM

0

On Oct 13, 6:08 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
> On 2008-10-13 14:38, Gianni Mariani wrote:
> > Stephen Horne wrote:
> >> On Mon, 13 Oct 2008 09:12:09 +0200, Michael DOUBEZ
> >> <michael.dou...@free.fr> wrote:

> >>> Look up template-dependant name:
> >>>http://www.parashift.com/c++-faq-lite/templates.html...

> >> Just read 35.18, 35.19 and 35.20. Now I *know* that the
> >> lunatics are running the asylum. Different scoping/lookup
> >> rules depending on whether something is "dependent" or
> >> "non-dependent" - pathetic. Next it'll be phases of the
> >> moon.

> >> Anyway, now that's done, I just have to figure out why GCC
> >> won't let me use offsetof in a constant expression - ie in
> >> exactly the kind of place you're always going to use
> >> offsetof. Where the last lot was an "oh well, a few hours
> >> hunting and fixing" thing, this is a real killer.

> > GCC does not let you use it as a constant because the
> > standard says so. SUCCS, I know, just is. You may get some
> > luck out of using pointer to member types.

> What is it that I'm missing, offsetof is a macro, it should be
> expanded before the compiler can even begin worrying about
> whether an expression is constant or not.

I'm just guessing, because of course, g++ lets you use a legal
offsetof as a constant, but I'll bet he's actually trying to use
it on something that isn't a PODs, which is undefined behavior.
In that case, g++ generates an error; a lot of compilers will
just give you some random (constant) value.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Stephen Horne

10/14/2008 2:09:00 AM

0

On Mon, 13 Oct 2008 12:38:51 GMT, Gianni Mariani
<gi4nospam@mariani.ws> wrote:

>Stephen Horne wrote:
>> On Mon, 13 Oct 2008 09:12:09 +0200, Michael DOUBEZ
>> <michael.doubez@free.fr> wrote:
>>
>>> Look up template-dependant name:
>>> http://www.parashift.com/c++-faq-lite/templates.html...

>> Anyway, now that's done, I just have to figure out why GCC won't let
>> me use offsetof in a constant expression - ie in exactly the kind of
>> place you're always going to use offsetof. Where the last lot was an
>> "oh well, a few hours hunting and fixing" thing, this is a real
>> killer.
>
>GCC does not let you use it as a constant because the standard says so.
>SUCCS, I know, just is. You may get some luck out of using pointer to
>member types.

No - GCC didn't let me use it because it didn't know that the
class/struct parameter was a class/struct, reason being I needed to
add 'typename'. GCC was entirely in the right, and I'd even count it
as a pretty sane aspect of the standard.

It's a bit strange, actually. The errors suggested that GCC does a
pointer-casting macro expansion for offsetof, but as far as I can
tell, what it's supposed to do is macro-expand to a built-in along the
lines of __offsetof__ (that's probably spelt wrong, but you get the
idea).

Anyway, turns out that's the easier thing to fix. Some of the
dependent stuff inheritance problems aren't so easy to fix - when they
work in GCC, they break in VC++ and visa versa. I feel superfluous
private inline access methods with #ifdef bodies approaching in the
near future.

Pointer-to-member types are a problem for several reasons, portability
being one of them since VC++ and probably others need you to drop
nonstandard hints about whether the class is single inheritance,
multiple inheritance or whatever. And when you do use them, the syntax
is a mess, and no-one can read it because no-one uses them enough to
get that used to them. Another one of those untested ideas the
standards people dreamed up, that don't really work in practice.

Stephen Horne

10/14/2008 2:58:00 AM

0

On Mon, 13 Oct 2008 10:08:32 -0700 (PDT), James Kanze
<james.kanze@gmail.com> wrote:

>On Oct 13, 6:08 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:

>> What is it that I'm missing, offsetof is a macro, it should be
>> expanded before the compiler can even begin worrying about
>> whether an expression is constant or not.
>
>I'm just guessing, because of course, g++ lets you use a legal
>offsetof as a constant, but I'll bet he's actually trying to use
>it on something that isn't a PODs, which is undefined behavior.
>In that case, g++ generates an error; a lot of compilers will
>just give you some random (constant) value.

No.

Read for tone and you'll see I'm seriously pissed off and frustrated,
and that thing about offsetof was ranting rather than a properly
organised request for help. For example, no quoted error messages.

Anyway, I said that GCC wouldn't let me use offsetof in a constant
expression, which *was* perfectly true. I also said "now I have to
figure out why" which I have since done.

The GCC errors directed me to lines that contained nothing much more
than offsetof, and complained about pointer operators etc that weren't
there (hence macro expansion). I just recently read the part of the
GCC manual that tells me it defines offsetof to map to a builtin,
unless I dreamed that up in a stress-related psychotic episode (it's
pretty bad when you hallucinate about reading manuals), making the
macro-expansion-implying errors seem strange.

Some other code relating to alignment used hand-written code that
effectively does an offsetof. When GCC complained about that, I fixed
it by simply using offsetof instead. This seems to suggest GCC is
actually doing something different to macro expansion, since replacing
the code that the macro is traditional expanded to with the macro gave
different behaviour. I guess it doesn't really matter, so long as
offsetof works, which it does - my problem was due to being in a
template again, and the need for "typename", which is perfectly
reasonable for a change.


You've got my interest, though. Why on earth should offsetof for a
non-POD struct/field be undefined? POD vs. non-POD shouldn't change
the layout of a struct. A non-POD struct might act as if it holds
extra data, though that data is external to the struct, but that has
nothing to do with offsetof. Some fields may be private and
inaccessible to offsetof (a compile-time error), some fields may be
pointers or references (not an error - just means you're referring to
the pointer), but that applies to POD and non-POD equally.

Also, it's not as if the macro expansion ever instantiates the type.
It just "imagines" a hypothetical instance at address zero and takes
the address of the field.

About the only thing I can think of which could *almost* legitimately
screw up offsetof would be overriding the * or -> pointer dereference
operators, but even if a macro expansion of offsetof uses ->, it uses
it with a *pointer* left argument rather than the non-pointer object,
so the override is irrelevant. Same goes if the macro uses * and .
instead.

Can you give me a reference to look this up?

I mean, the idea that I can't take the offsetof a field in a data
structure node just because the application is using a non-POD type
for the contained data is beyond ridiculous.

In keeping with my generally pissed-off tone, I'll also ask if the
standards people came up with this one specifically to drive me nuts?
Or are you just baiting me for fun?

James Kanze

10/14/2008 8:29:00 AM

0

On Oct 14, 4:58 am, Stephen Horne <sh006d3...@blueyonder.co.uk> wrote:
> On Mon, 13 Oct 2008 10:08:32 -0700 (PDT), James Kanze

> <james.ka...@gmail.com> wrote:
> >On Oct 13, 6:08 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
> >> What is it that I'm missing, offsetof is a macro, it should
> >> be expanded before the compiler can even begin worrying
> >> about whether an expression is constant or not.

> >I'm just guessing, because of course, g++ lets you use a
> >legal offsetof as a constant, but I'll bet he's actually
> >trying to use it on something that isn't a PODs, which is
> >undefined behavior. In that case, g++ generates an error; a
> >lot of compilers will just give you some random (constant)
> >value.

> No.

> Read for tone and you'll see I'm seriously pissed off and
> frustrated, and that thing about offsetof was ranting rather
> than a properly organised request for help. For example, no
> quoted error messages.

I understood that you were pissed off. Still, g++ is fully
conform with regards to offsetof, and does allow it's use in an
integral constant expression. So presumably, your real problem
is elsewhere. And since most compilers don't complain about use
of offsetof on a non-POD, but g++ does, I made a guess that that
might be the real problem.

> Anyway, I said that GCC wouldn't let me use offsetof in a
> constant expression, which *was* perfectly true.

No it's not. The following code compiles perfectly well with
g++:

#include <cstddef>
#include <iostream>

struct S
{
int i ;
int j ;
int k ;
} ;

int
main()
{
char a[ offsetof( S, k ) ] ;
std::cout << sizeof( a ) << std::endl ;
return 0 ;
}

You're doing something else wrong.

> I also said "now I have to figure out why" which I have since
> done.

> The GCC errors directed me to lines that contained nothing
> much more than offsetof, and complained about pointer
> operators etc that weren't there (hence macro expansion). I
> just recently read the part of the GCC manual that tells me it
> defines offsetof to map to a builtin, unless I dreamed that up
> in a stress-related psychotic episode (it's pretty bad when
> you hallucinate about reading manuals), making the
> macro-expansion-implying errors seem strange.

> Some other code relating to alignment used hand-written code
> that effectively does an offsetof. When GCC complained about
> that, I fixed it by simply using offsetof instead. This seems
> to suggest GCC is actually doing something different to macro
> expansion, since replacing the code that the macro is
> traditional expanded to with the macro gave different
> behaviour. I guess it doesn't really matter, so long as
> offsetof works, which it does - my problem was due to being in
> a template again, and the need for "typename", which is
> perfectly reasonable for a change.

Fine. The problem wasn't due to g++'s expansion of offsetof not
being a constant integral expression; it was due to a syntax
error elsewhere.

For the record, g++ defines offsetof as a macro (as required by
the standard), but that macro expands to something like
"__builtin_offsetof( t, e )" (which is, IMHO, the only
reasonable way of implementing it). So what you actually get is
a compiler built in, which is either a constant integral
expression, or causes an error, depending on whether the use of
offsetof is legal or not.

> You've got my interest, though. Why on earth should offsetof
> for a non-POD struct/field be undefined?

Because it can't be implemented in the general case, and no one
considered it worth the effort of specifying when it would be
legal, and when not, except for a PODS, which is all that is
necessary for C compatibility (and the only reason it's there is
for reasons of C compatibility).

> POD vs. non-POD shouldn't change the layout of a struct.

In some cases of inheritance (particularly where virtual
inheritance is involved), the layout is dynamically defined; the
compiler doesn't know it. And the "classical" implementation of
offsetof in C is something like:

#define offsetof( S, e ) ((size_t)(&(((S*)0)->e)))

Which is undefined behavior if you write it, but which the
library implementor can do if he knows that it will get by the
compiler.

Many libraries (e.g. Sun CC, VC++) still use something like
this, and the standards committee didn't want to ban it. And it
fails as soon as the member is private.

> A non-POD struct might act as if it holds extra data, though
> that data is external to the struct, but that has nothing to
> do with offsetof. Some fields may be private and inaccessible
> to offsetof (a compile-time error), some fields may be
> pointers or references (not an error - just means you're
> referring to the pointer), but that applies to POD and non-POD
> equally.

References could also cause problems. In general, it would
doubtlessly be possible to loosen the rules somewhat. Doing so
would require a fairly detailed analysis, however, to ensure
that the new rules didn't cause any problem for potential
implementations, and no one on the committee felt the effort was
worthwhile. (The case of POD was "established practice", from
C.)

> Also, it's not as if the macro expansion ever instantiates the
> type. It just "imagines" a hypothetical instance at address
> zero and takes the address of the field.

> About the only thing I can think of which could *almost*
> legitimately screw up offsetof would be overriding the * or ->
> pointer dereference operators, but even if a macro expansion
> of offsetof uses ->, it uses it with a *pointer* left argument
> rather than the non-pointer object, so the override is
> irrelevant. Same goes if the macro uses * and . instead.

> Can you give me a reference to look this up?

To look what up? The standard says quite clearly (§18.1/5):

The macro offsetof accepts a restricted set of type
arguments in this International Standard. type shall be
a POD structure or a a POD union. The result of
applying the offsetof macro to a field that is a static
data member or a function member is undefined.

> I mean, the idea that I can't take the offsetof a field in a
> data structure node just because the application is using a
> non-POD type for the contained data is beyond ridiculous.

No. It's a perfectly reasonable constraint.

> In keeping with my generally pissed-off tone, I'll also ask if
> the standards people came up with this one specifically to
> drive me nuts? Or are you just baiting me for fun?

Actually, they're just trying to strike a compromise between the
ideal solution (drop the macro entirely) and C compatibility.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze

10/14/2008 8:35:00 AM

0

On Oct 14, 4:08 am, Stephen Horne <sh006d3...@blueyonder.co.uk> wrote:
> On Mon, 13 Oct 2008 12:38:51 GMT, Gianni Mariani
> <gi4nos...@mariani.ws> wrote:

[...]
> Pointer-to-member types are a problem for several reasons,
> portability being one of them since VC++ and probably others
> need you to drop nonstandard hints about whether the class is
> single inheritance, multiple inheritance or whatever.

That's not quite true. VC++ does require special compiler
options to truly be a C++ compiler (but that's true of all
compilers I know), but if you use them (/vmg, in this case),
pointers to members cause no problems. (At least that I can
tell; most of my experience with pointers to members has been
with pointer to member functions, which definitly don't work
with VC++ unless you specify this option.)

> And when you do use them, the syntax is a mess, and no-one can
> read it because no-one uses them enough to get that used to
> them. Another one of those untested ideas the standards people
> dreamed up, that don't really work in practice.

Sort of. In practice, there just isn't that much use for
pointers to members, so there's really no need for a
particularly simple syntax. And the syntax for using pointers
to a data member isn't that bad, either, although I can see
people getting a bit bothered when pointers to member functions
are involved.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34