Thread overview
Constant initialization
Dec 09, 2004
Tyro
Dec 09, 2004
Simon Buchan
Dec 09, 2004
Tyro
Dec 09, 2004
Simon Buchan
Dec 11, 2004
Garett Bass
December 09, 2004
Is there a reason why a const cannot be initialized by the run-time return values of a function call? If there is no "good" reason, can we have this feature?

void main()
{ const int eint = 5 + 5;        // Legal
  const int rint = 5 + getInt(); // Illegal }

int getInt()
{ return 5; }

Tyro
December 09, 2004
On Thu, 09 Dec 2004 04:43:23 -0500, Tyro <ridimz_at@yahoo.dot.com> wrote:

> Is there a reason why a const cannot be initialized by the run-time return values of a function call? If there is no "good" reason, can we have this feature?
>
> void main()
> { const int eint = 5 + 5;        // Legal
>    const int rint = 5 + getInt(); // Illegal }
>
> int getInt()
> { return 5; }
>
> Tyro

What exactly are you asking here, that the compiler figure out that your getInt()
function always returns 5 and swaps it out for a constant 5? Or do you want "Semi"
const values: set once at the program start by running that code?

The first is a bit too much to ask, but the effect can be achived with fancy templating,
and the second is covered by static constructors. (The documentation is your friend here)

-- 
"Unhappy Microsoft customers have a funny way of becoming Linux,
Salesforce.com and Oracle customers." - www.microsoft-watch.com:
"The Year in Review: Microsoft Opens Up"
--
"I plan on at least one critical patch every month, and I haven't been disappointed."
- Adam Hansen, manager of security at Sonnenschein Nath & Rosenthal LLP
(Quote from http://www.eweek.com/article2/0,1759,1736104,00.asp)
--
"It's been a challenge to "reteach or retrain" Web users to pay for content, said Pizey"
-Wired website: "The Incredible Shrinking Comic"
December 09, 2004
In article <opsiqt2fdrjccy7t@simon.mshome.net>, Simon Buchan says...
>
>On Thu, 09 Dec 2004 04:43:23 -0500, Tyro <ridimz_at@yahoo.dot.com> wrote:
>
>> Is there a reason why a const cannot be initialized by the run-time return values of a function call? If there is no "good" reason, can we have this feature?
>>
>> void main()
>> { const int eint = 5 + 5;        // Legal
>>    const int rint = 5 + getInt(); // Illegal }
>>
>> int getInt()
>> { return 5; }
>>
>> Tyro
>
>What exactly are you asking here, that the compiler figure out that your
>getInt()
>function always returns 5 and swaps it out for a constant 5? Or do you
>want "Semi"
>const values: set once at the program start by running that code?
>
>The first is a bit too much to ask, but the effect can be achived with
>fancy templating,
>and the second is covered by static constructors. (The documentation is
>your friend here)

I want the compler to allow me to use the return value of a function to initialize a constant variable.

I want to ask the user for input that I can use to initialize a variable who's value will be constant from that point on. Only way I know to do that is through the return value of a function.

int main ()
{ const int LENGTH = getLength();
char[LENGTH] myString; }

int getLength()
{ int length;
std.stream.stdin.scanf("%d",&length);
return length; }

If there is some other way to accomplish this, please let me know.

Andrew

>"Unhappy Microsoft customers have a funny way of becoming Linux, Salesforce.com and Oracle customers." - www.microsoft-watch.com: "The Year in Review: Microsoft Opens Up"
>--
>"I plan on at least one critical patch every month, and I haven't been
>disappointed."
>- Adam Hansen, manager of security at Sonnenschein Nath & Rosenthal LLP
>(Quote from http://www.eweek.com/article2/0,1759,1736104,00.asp)
>--
>"It's been a challenge to "reteach or retrain" Web users to pay for
>content, said Pizey"
>-Wired website: "The Incredible Shrinking Comic"


December 09, 2004
On Thu, 9 Dec 2004 14:21:10 +0000 (UTC), Tyro <Tyro_member@pathlink.com> wrote:

<snip>
>
> I want the compler to allow me to use the return value of a function to
> initialize a constant variable.
>
> I want to ask the user for input that I can use to initialize a variable who's
> value will be constant from that point on. Only way I know to do that is through
> the return value of a function.
>
> int main ()
> { const int LENGTH = getLength();
> char[LENGTH] myString; }
>
> int getLength()
> { int length;
> std.stream.stdin.scanf("%d",&length);
> return length; }
>
> If there is some other way to accomplish this, please let me know.
>
> Andrew
>
>> "Unhappy Microsoft customers have a funny way of becoming Linux,
>> Salesforce.com and Oracle customers." - www.microsoft-watch.com:
>> "The Year in Review: Microsoft Opens Up"
>> --
>> "I plan on at least one critical patch every month, and I haven't been
>> disappointed."
>> - Adam Hansen, manager of security at Sonnenschein Nath & Rosenthal LLP
>> (Quote from http://www.eweek.com/article2/0,1759,1736104,00.asp)
>> --
>> "It's been a challenge to "reteach or retrain" Web users to pay for
>> content, said Pizey"
>> -Wired website: "The Incredible Shrinking Comic"
>
>

Not as far as I know, unless you like the idea of doing everything in a
function and passing it that value, like:

main() {
	int input;
	readf(input);
	do_everything(input);
}

void do_everything(const int input) {...}

const is used to determine whether something can be determined at runtime,
though, so your best bet is to just try to not write to it. If you REALLY
want it safe, though, you could wrap in in a class:

class constInt {
	private:
		int _me;
	public:
		this(int init) {
			_me = init;
		}
		int me() {return _me;}
}

(this is where something along the lines of a opCast would be usefull (or
is it already?)

If you are worried about library functions changing it, if they don't NEED
to change it, they can't, if they do, you don't want to use it anyway.

-- 
"Unhappy Microsoft customers have a funny way of becoming Linux,
Salesforce.com and Oracle customers." - www.microsoft-watch.com:
"The Year in Review: Microsoft Opens Up"
--
"I plan on at least one critical patch every month, and I haven't been disappointed."
- Adam Hansen, manager of security at Sonnenschein Nath & Rosenthal LLP
(Quote from http://www.eweek.com/article2/0,1759,1736104,00.asp)
--
"It's been a challenge to "reteach or retrain" Web users to pay for content, said Pizey"
-Wired website: "The Incredible Shrinking Comic"
December 11, 2004
IIRC, this is how final variables work in Java.  It is handy, since you can store data once, and guarantee it will not be altered subsequently.

"Tyro" <ridimz_at@yahoo.dot.com> wrote in message news:cp96o3$2j41$1@digitaldaemon.com...
> Is there a reason why a const cannot be initialized by the run-time return values of a function call? If there is no "good" reason, can we have this feature?
>
> void main()
> { const int eint = 5 + 5;        // Legal
>   const int rint = 5 + getInt(); // Illegal }
>
> int getInt()
> { return 5; }
>
> Tyro