Hi:
I have the following simple program:
#include<iostre am>
using namespace std;
int main(int argc, char* argv[]){
const double L = 1.234;
const int T = static_cast<con st int>(L);
int arr[T];
return 0;
}
But I get the error message shown in title. Why doesn't my program
work? Thanks for help! 13 27437
On 2007-01-20 17:08, hn********@gmai l.com wrote:
Hi:
I have the following simple program:
#include<iostre am>
using namespace std;
int main(int argc, char* argv[]){
const double L = 1.234;
const int T = static_cast<con st int>(L);
int arr[T];
return 0;
}
But I get the error message shown in title. Why doesn't my program
work? Thanks for help!
Because T is not constant at compile-time, which it needs to be for the
compiler to know how much space to allocate on the stack. That, of
course, is not the correct explanation according to the standard but it
quite nicely describes what is going on. Consider the following code in
which T is also constant but can wary with every run of the application:
#include<iostre am>
int main()
{
double L;
std::cin >L;
const int T = static_cast<con st int>(L);
return 0;
}
--
Erik Wikström hn********@gmai l.com wrote:
Hi:
I have the following simple program:
#include<iostre am>
using namespace std;
int main(int argc, char* argv[]) {
const double L = 1.234;
const int T = static_cast<con st int>(L);
int arr[T];
return 0;
}
Why doesn't my program work?
Good question. The code compiled fine for my g++ compiler, but not for http://www.comeaucomputing.com/tryitout I have some questions about
comeau's output:
"ComeauTest .c", line 6: warning: type qualifier is meaningless on cast type
const int T = static_cast<con st int>(L);
^
What is this? Isn't the 'const' required in the above context?
"ComeauTest .c", line 7: error: constant value is not known
int arr[T];
^
Why is that?
Erik Wikström wrote:
On 2007-01-20 17:08, hn********@gmai l.com wrote:
>Hi: I have the following simple program:
#include<iostre am> using namespace std; int main(int argc, char* argv[]){
const double L = 1.234; const int T = static_cast<con st int>(L); int arr[T]; return 0; }
But I get the error message shown in title. Why doesn't my program work? Thanks for help!
Because T is not constant at compile-time,
Huh? How is that?
which it needs to be for the
compiler to know how much space to allocate on the stack. That, of
course, is not the correct explanation according to the standard but it
quite nicely describes what is going on. Consider the following code in
which T is also constant but can wary with every run of the application:
#include<iostre am>
int main()
{
double L;
You are cheating in the line above. The OP had:
const double L = 1.234;
^^^^^
std::cin >L;
With the line of the OP, this would be undefined behavior.
const int T = static_cast<con st int>(L);
return 0;
}
Best
Kai-Uwe Bux
Daniel T. wrote:
hn********@gmai l.com wrote:
>Hi: I have the following simple program:
#include<iostre am> using namespace std; int main(int argc, char* argv[]) { const double L = 1.234; const int T = static_cast<con st int>(L); int arr[T]; return 0; }
Why doesn't my program work?
Good question. The code compiled fine for my g++ compiler, but not for http://www.comeaucomputing.com/tryitout I have some questions about
comeau's output:
>"ComeauTest.c" , line 6: warning: type qualifier is meaningless on cast type const int T = static_cast<con st int>(L); ^
What is this? Isn't the 'const' required in the above context?
>"ComeauTest.c" , line 7: error: constant value is not known int arr[T]; ^
Why is that?
In order for T to be a compile-time constant, it has to have an
initializer that's a compile-time constant. Floating-point values aren't.
--
-- Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." ( www.petebecker.com/tr1book)
Pete Becker <pe**@versatile coding.comwrote :
Daniel T. wrote:
hn********@gmai l.com wrote:
Hi:
I have the following simple program:
>
#include<iostre am>
using namespace std;
int main(int argc, char* argv[]) {
const double L = 1.234;
const int T = static_cast<con st int>(L);
int arr[T];
return 0;
}
>
Why doesn't my program work?
Good question. The code compiled fine for my g++ compiler, but not for http://www.comeaucomputing.com/tryitout I have some questions about
comeau's output:
"ComeauTest .c", line 6: warning: type qualifier is meaningless on cast type
const int T = static_cast<con st int>(L);
^
What is this? Isn't the 'const' required in the above context?
What about this warning?
"ComeauTest .c", line 7: error: constant value is not known
int arr[T];
^
Why is that?
In order for T to be a compile-time constant, it has to have an
initializer that's a compile-time constant. Floating-point values aren't.
I didn't know that. Thanks.
"Erik Wikström 写é“:
"
On 2007-01-20 17:08, hn********@gmai l.com wrote:
Hi:
I have the following simple program:
#include<iostre am>
using namespace std;
int main(int argc, char* argv[]){
const double L = 1.234;
const int T = static_cast<con st int>(L);
int arr[T];
return 0;
}
But I get the error message shown in title. Why doesn't my program
work? Thanks for help!
Because T is not constant at compile-time, which it needs to be for the
compiler to know how much space to allocate on the stack. That, of
course, is not the correct explanation according to the standard but it
quite nicely describes what is going on. Consider the following code in
which T is also constant but can wary with every run of the application:
#include<iostre am>
int main()
{
double L;
std::cin >L;
const int T = static_cast<con st int>(L);
return 0;
}
--
Erik Wikström
Thanks for help, and here I've made some modification to the program.
#include<iostre am>
int main()
{
#define L 1.234
#define T static_cast<con st int>(L)
int arr[T];
return 0;
}
Does it mean that now T is a "compile-time determined" variable?
If it does, then could I suppose that "#define" is expanded and
calculated at complie-time?
Does all "#define" perform in a same way?
Thanks a lot for helping me.
Daniel T. wrote:
Pete Becker <pe**@versatile coding.comwrote :
>Daniel T. wrote:
>>hn********@gmai l.com wrote:
Hi: I have the following simple program:
#include<iostre am> using namespace std; int main(int argc, char* argv[]) { const double L = 1.234; const int T = static_cast<con st int>(L); int arr[T]; return 0; }
Why doesn't my program work? Good question. The code compiled fine for my g++ compiler, but not for http://www.comeaucomputing.com/tryitout I have some questions about comeau's output:
"ComeauTest. c", line 6: warning: type qualifier is meaningless on cast type const int T = static_cast<con st int>(L); ^ What is this? Isn't the 'const' required in the above context?
What about this warning?
For built-in types, there is no difference between a const rvalue and a
non-const rvalue. That means that there is no difference between the
following casts (they both produce an rvalue of type int):
static_cast<int >(1.234)
static_cast<con st int>(1.234)
So the const is meaningless in this context.
--
Clark S. Cox III cl*******@gmail .com
On Jan 20, 8:10 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
Erik Wikström wrote:
On 2007-01-20 17:08, hn.ft.p...@gmai l.com wrote:
Hi:
I have the following simple program:
#include<iostre am>
using namespace std;
int main(int argc, char* argv[]){
const double L = 1.234;
const int T = static_cast<con st int>(L);
int arr[T];
return 0;
}
But I get the error message shown in title. Why doesn't my program
work? Thanks for help!
Because T is not constant at compile-time,
Huh? How is that?
Because the program have to execute the static_cast before the value of
T can be determined. And to declare an array on the stack you need to
know the size of the array at compile-time. Perhaps I was a bit
unclear, what I meant was that T was not a constant value at
compile-time.
which it needs to be for the
compiler to know how much space to allocate on the stack. That, of
course, is not the correct explanation according to the standard but it
quite nicely describes what is going on. Consider the following code in
which T is also constant but can wary with every run of the application:
#include<iostre am>
int main()
{
double L;
You are cheating in the line above. The OP had:
const double L = 1.234;
^^^^^
Yes, but it's the value (and constness) of T that is of interest in
this problem.
--
Erik Wikström
On Jan 21, 10:51 am, hn.ft.p...@gmai l.com wrote:
Thanks for help, and here I've made some modification to the program.
#include<iostre am>
int main()
{
#define L 1.234
#define T static_cast<con st int>(L)
int arr[T];
return 0;
}
Does it mean that now T is a "compile-time determined" variable?
No, what you have done is to use macros to restructure your code a bit,
after this has been through the pre-processor this is what you'll get:
#include<iostre am>
int main()
{
int arr[static_cast<con st int>(1.234)];
return 0;
}
If it does, then could I suppose that "#define" is expanded and
calculated at compile-time?
They are expanded, yes, but not calculated. Or rather the static_cast
is not evaluated until runtime, which is to late since the size of the
array needs to be known at compile-time.
Does all "#define" perform in a same way?
All defines are expanded by the pre-processor, but that is all, if you
want code evaluated at compile-time you can do some things with
templates, but I don't think that's what you want.
What you can do is to allocate the array on the heap:
int main()
{
int* arr = new int[static_cast<con st int>(1.234)];
delete[] arr; // don't forget this when you are done
return 0;
}
--
Erik Wikström This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: hvaisane |
last post by:
Valgrind says
==11604== Invalid read of size 4
==11604== at 0x8048ABB: main (foo.cc:36)
==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd
==11604== at 0x1B90514F: operator delete(void*) (vg_replace_malloc.c:156)
==11604== by 0x804A1BA: __gnu_cxx::new_allocator<Foo>::deallocate(Foo*, unsigned) (new_allocator.h:86)
==11604== by 0x8049C08: std::_Vector_base<Foo, std::allocator<Foo> >::_M_deallocate(Foo*,...
|
by: mrwoopey |
last post by:
Hi,
I am using the example "Authenticate against the Active Directory by
Using Forms Authentication and Visual Basic .NET":
http://support.microsoft.com/default.aspx?scid=KB;EN-US;326340
But I am having a problem figuring out the LDAP://
The LDAP:// that I pass looks like this (i substitued generic the
|
by: drako |
last post by:
Hi,
I'm a bit stumped as I am getting a "Notice: Array to String
Conversion" error when trying to do something that on the surface
should be a very simple task - create an array, and write a set of
values to them based on data submitted from POST Fields.
Code below:
$_SESSION = array();
|
by: jaime |
last post by:
Hi again all.
Given the line:
const int x=5;
Can I then use "x" as a constant expression? (By "constant expression", I
mean "constant expression" as defined in the C99 standard)
I've been searching google for 2 days now trying to answer this myself,
and I'm just getting more and more confused (some things I read make me
think "yes", while some things I read make me think "no").
|
by: Lawrence Spector |
last post by:
I ran into a problem using g++. Visual Studio 2005 never complained
about this, but with g++ I ran into this error. I can't figure out if
I've done something wrong or if this is a compiler bug. Here's a very
simple example which should illustrate what I'm doing.
#include <iostream>
template <class T>
class TestBase
{
| |
by: Adem |
last post by:
C/C++ language proposal:
Change the 'case expression' from "integral constant-expression" to "integral expression"
The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under 6.4.2(2) :
case constant-expression :
I propose that the case expression of the switch statement
be changed from "integral constant-expression" to "integral expression".
|
by: Godlove Mathew |
last post by:
Hi, I installed a Borland C++ in my laptop a 32-bit operating system with windows vista.
I wrote i very simple program to display "Hello world" but it gives an error
// my first program in C++
#include <iostream>
using namespace std;
int main ()
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |