Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

¦³ c++ °ò¥»°ÝÃD·Q°Ý...

0 views
Skip to first unread message

canda

unread,
Mar 2, 2000, 3:00:00 AM3/2/00
to

void main()
{
int a =9;
print(a);
}

void print(const int& x)
{ ^^^^^^ ------>這裡宣告 const, 有必要嗎 ?
cout << x...
}

若不加 const, 會有影響嗎 ?

--
[1;32m※ Origin: [33m交大資工鳳凰城資訊站 [37m<bbs.csie.nctu.edu.tw> [m
[1;31m◆ From: [36mtrt-on61-48.netcom.ca [m

難得的恐龍

unread,
Mar 3, 2000, 3:00:00 AM3/3/00
to
※ 引述《freak...@bbs.csie.nctu.edu.tw (jon)》之銘言:
> ※ 引述《rosefo...@bbs.kimo.com.tw (難得的恐龍)》之銘言:
> > � Are you sure ? Can anyone explain it ?
> > But I think "const" somewhat helps to
> > optimum the memory layout of some variable
> > declared as const in compile phase .
> Occasionally, const does help compilers generate more efficient code.
> However, that's not the purpose of const, and using const does
> not guarantee the compiler would do any optimization.

Would you present some program fragments involved
const phrase that compiler need not do explictly
optimization ? (with implementer-independent )


--
[1;33m※ Origin: [36m奇摩 大摩域 [37m<telnet://bbs.kimo.com.tw> [m
[1;35m◆ From: [1;32m210.63.66.73 [m

jon

unread,
Mar 3, 2000, 3:00:00 AM3/3/00
to
※ 引述《rosefo...@bbs.kimo.com.tw (難得的恐龍)》之銘言:
> ※ 引述《freak...@bbs.csie.nctu.edu.tw (jon)》之銘言:

> > Occasionally, const does help compilers generate more efficient code.
> > However, that's not the purpose of const, and using const does
> > not guarantee the compiler would do any optimization.
> Would you present some program fragments involved
> const phrase that compiler need not do explictly
> optimization ? (with implementer-independent )

The C++ Standard does not require compilers to do any optimization
against const at all! It is because an object declared as const
cannot be modified during its lifetime, under certain circumstances
the compiler could do some optimization. Occasionally, but not always.


--
[1;32m※ Origin: [33m交大資工鳳凰城資訊站 [37m<bbs.csie.nctu.edu.tw> [m

[1;31m◆ From: [36mt202-234.dialup.seed.net.tw [m

蕭邦的異想世界

unread,
Mar 3, 2000, 3:00:00 AM3/3/00
to
※ 引述《freak...@bbs.csie.nctu.edu.tw (jon)》之銘言:
: ※ 引述《rosefo...@bbs.kimo.com.tw (難得的恐龍)》之銘言:
: > Would you present some program fragments involved

: > const phrase that compiler need not do explictly
: > optimization ? (with implementer-independent )
: The C++ Standard does not require compilers to do any optimization
: against const at all! It is because an object declared as const
: cannot be modified during its lifetime, under certain circumstances
: the compiler could do some optimization. Occasionally, but not always.

如果一個全域變數的值在程式執行過程中都不需要改變, 例如:

const double pi=3.14;

這樣是有可能產生比較有效率的碼. 程式中運算式裡的 pi 可以直接用 3.14 取代.
(如果可以的話)

但是如果 const 出現在函式的參數列中作為參數的型別修飾, 像這樣:

int etude(const int op, const char *composer);

那... 實在不知道編譯器針對這樣子的 const 有什麼最佳化動作可以進行? ~_~

--
※ Origin: 程式設計樂園 ◆ From: h11.s35.ts31.hinet.net

0 new messages