By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,199 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Three programs with sizeof

P: n/a
Here are three programs.

--- foo1.cpp ---
int foo;
int main()
{
struct foo {};
sizeof (foo);
return 0;
}
----------------

--- foo2.cpp ---
int foo;
int main()
{
struct foo {};
sizeof foo; // Line#5
return 0;
}
----------------

--- foo3.cpp ---
int foo;
int main()
{
sizeof foo;
return 0;
}
----------------
=== Compilation ===

$ g++ --version
g++ (GCC) 3.3.1 (cygming special)
[---omitted---]
$ g++ foo1.cpp
// No errors, no warnings
$ g++ foo2.cpp
foo2.cpp: In function `int main()':
foo2.cpp:5: error: syntax error before `;' token
$ g++ foo3.cpp
// No errors, no warnings

===================
Is g++ right while compiling foo2.cpp?
Why doesn't the compiler see 'int foo'?
--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Alex Vinokur"
Is g++ right while compiling foo2.cpp?
Why doesn't the compiler see 'int foo'?


It is correct with the error since the syntax for using sizeof with a
type-name is to have brackets around the type-name. Name lookup rules is
the answer to the second question. If this is wrong then do correct me.

Fraser.
Jul 22 '05 #2

P: n/a

"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message news:41******@news.greennet.net...

"Alex Vinokur"
> Is g++ right while compiling foo2.cpp?
Why doesn't the compiler see 'int foo'?


It is correct with the error since the syntax for using sizeof with a
type-name is to have brackets around the type-name. Name lookup rules is
the answer to the second question. If this is wrong then do correct me.

You're quite right. Syntactically in the problem case foo is a typename.
"sizeof foo" is therefore ill-formed. Typename arguments sizeof need
parentheses around them.

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.