-
Ruined Reputation: static const int...
I put my rep on the line was completely embarrassed.
First, I used a static const int in a function to size an array:
Code:
int function()
{
static const int SIZE(10);
char buf[SIZE];
...
}
This works on some versions of g++, but a guy running g++ (version 4. something) on a Lynux box had a error for it. The error indicated that char buf was being sized with a non-constant integral expression.
The compiler, for some reason, was not able to determine that SIZE should be replaced with 10.
Furthermore, C++ style teaches to use "static const int..." instead of #define. At least I was taught that.
I thought that not only did this enforce type safety, but any good compiler would optimize it away and you wouldn't waste space.
A guy here told me he put a "static const int ..." in a .h file and a separate copy of the const was created in each compilation unit that included the .h.
I think these are compiler errors. What say ye?
-
Actually, your reputation is untarnished -- something I can't say about your compiler, though.
The code is fine and should compile under any C++ compiler, including a C++98 compiler. I know that GNU have had some bugs with static const data, and I guess this bug is probably discussed on one of their forums (I'll have to check it) but for the time being, you can rest assure that this is perfectly valid C++ code. Try to replace the parentheses with a = sign. Perhaps this will convince the compiler that SIZE is a constant expression...
Last edited by Danny; 07-15-2009 at 05:10 PM.
Danny Kalev
-
Actually, I should have put in the initial post that when I substituted = for the constructor form for initializing the constant, the Compiler was happy.
On the second issue, I found out that the guy used "no optimization" when compiling. No wonder the objects were not optimized away.
-
Even if the object weren't optimized away, they still make a const expression so the code should compiler in either case (i.e., optimized and unoptimized).
Danny Kalev
Similar Threads
-
By Max_Payne in forum C++
Replies: 1
Last Post: 02-15-2010, 10:21 AM
-
By Ranger in forum VB Classic
Replies: 3
Last Post: 05-29-2006, 09:43 PM
-
By lordanki in forum Java
Replies: 3
Last Post: 04-04-2006, 10:13 AM
-
By Ranger in forum VB Classic
Replies: 0
Last Post: 12-09-2000, 12:21 PM
-
By Kunal Sharma in forum VB Classic
Replies: 2
Last Post: 04-25-2000, 03:45 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks