[問題] the C++ programming language裡面的問題
大家好, 最近剛好翻到the C++ programming language,章節C.13.8.4
有關templates, namespace 跟 specialization的, 看不太懂他所要表達的意思.
首先呢, 上面提到:
.......省略.......
This reflects three obvious strategies an implementation can use for
generating specializations:
[1] Generate a specialization the first time a call is seen.
[2] At the end of a translation unit, generate all specializations needed for
that translation unit.
[3] Once every translation unit of a program has been seen, generate all
specializations needed for the program.
All three strategies have strengths and weaknesses, and combinations of these
strategies are also possible.
......省略.......
然後說:
A program is illegal, if it is possible to construct two different meanings
by choosing different points of instantiation or different contents of
namespaces at different possible contexts for generating the specialization.
然後有一段範例碼:
namespace N
{
class A{ /* ... */ };
char f(A,int) ;
}
template<class T, class T2> char g(T t, T2 t2) { return f(t,t2) ; }
//error (alternative resolutions of f(t))
char c= g(N: :A() ,'a') ; //<---- specialization
namespace N //add to namespace N (§8.2.9.3
{
void f(A,char) ;
}
下面是我的疑惑:
對於文字解說, 我的理解是如果想像自己是compiler, 如果檢查最前面那三種產生
specialization的方法後, 發現產生的程式碼(例如像範例碼中的呼叫f(...))不同
則是一個編譯錯誤, 這邊的用詞是 the program is illegal.
但問題是compiler好像不太可能用三種方法產生程式碼之後再繼續後面的compiling吧?
(不管最後有沒有編譯錯誤)
實際跑了一下跟玩一下範例碼, 感覺就只是產生specialization那行, 實際上被放到
void f(A, char)被定義的namespace之後, 也就是說被放到所有dependent namespace N
的後面而已, 這樣的話compiler就能盡量找到所有可能適用的函數.....
不過用這個解釋就不知道最一開始提的那三點是要幹嘛用的啦?
看的有點糊塗 在此請各位先進指點一下~~~感謝~~~
其中一個疑惑就是那三點到底想表達的意思是什麼呢?
P.S. 如果把 void f(A,char) 改成 char f(A,char) 就沒編譯錯誤了
可是場景還是相同阿?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.224.44.2 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1562002106.A.976.html
※ 編輯: saladim (36.224.44.2 臺灣), 07/02/2019 01:29:31
※ 編輯: saladim (36.224.44.2 臺灣), 07/02/2019 01:31:38
※ 編輯: saladim (36.224.44.2 臺灣), 07/02/2019 01:32:08
推
07/02 04:58,
6年前
, 1F
07/02 04:58, 1F
→
07/02 04:58,
6年前
, 2F
07/02 04:58, 2F
→
07/02 04:58,
6年前
, 3F
07/02 04:58, 3F
→
07/02 23:11,
6年前
, 4F
07/02 23:11, 4F
→
07/02 23:12,
6年前
, 5F
07/02 23:12, 5F
→
07/02 23:13,
6年前
, 6F
07/02 23:13, 6F
→
07/02 23:13,
6年前
, 7F
07/02 23:13, 7F
→
07/02 23:49,
6年前
, 8F
07/02 23:49, 8F
→
07/02 23:50,
6年前
, 9F
07/02 23:50, 9F
→
07/03 02:28,
6年前
, 10F
07/03 02:28, 10F
→
07/03 02:30,
6年前
, 11F
07/03 02:30, 11F
→
07/03 02:31,
6年前
, 12F
07/03 02:31, 12F
推
07/03 07:45,
6年前
, 13F
07/03 07:45, 13F
→
07/03 07:45,
6年前
, 14F
07/03 07:45, 14F
→
07/03 07:45,
6年前
, 15F
07/03 07:45, 15F
→
07/03 07:45,
6年前
, 16F
07/03 07:45, 16F
→
07/03 07:45,
6年前
, 17F
07/03 07:45, 17F
→
07/03 07:45,
6年前
, 18F
07/03 07:45, 18F
→
07/03 07:45,
6年前
, 19F
07/03 07:45, 19F
→
07/03 07:45,
6年前
, 20F
07/03 07:45, 20F
→
07/03 07:45,
6年前
, 21F
07/03 07:45, 21F
→
07/03 07:45,
6年前
, 22F
07/03 07:45, 22F
→
07/03 07:45,
6年前
, 23F
07/03 07:45, 23F
→
07/03 07:45,
6年前
, 24F
07/03 07:45, 24F
推
07/03 07:47,
6年前
, 25F
07/03 07:47, 25F
→
07/03 09:12,
6年前
, 26F
07/03 09:12, 26F
→
07/03 09:13,
6年前
, 27F
07/03 09:13, 27F
→
07/03 09:13,
6年前
, 28F
07/03 09:13, 28F
→
07/03 09:13,
6年前
, 29F
07/03 09:13, 29F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章
33
68