Case 1: ParamType is a Reference or Pointer, but not a Universal Reference
What the compiler sees(using compiler insight):
func(x)
則對應到 int &
func(cx), func(rx)
都對應到 const int &
,注意到 const 也被 type deduction 考慮進去
若改成
則 func(x)
對應到的就會變成 const int &
,其他兩個因為本來就具有 const ,因此不變。
Case 2: ParamType is a Universal Reference
當 universal reference 遇到 l-value 時,template type deduction 會推斷出 l-value reference 的結果,而遇到 r-value 時,就不做特別處理。
從 universal reference 本身的意義來想,這樣做也很自然。
What the compiler sees(using compiler insight):
func(x)
則對應到 int &
func(cx), func(rx)
都對應到 const int &
func(28)
對應到 int &&
,因為 28 為 r-value
Case 3: ParamType is Neither a Pointer nor a Reference
What the compiler sees(using compiler insight):
- 全部都對應到
int
,const (even volatile) 都會被 ignore,因為是 copy-by-value,所以 const、volatile 其實都沒什麼意義,因為 const 是要保證原本的值不會被修改,而不是用來保證原本的值的 copy 不會被更動。
Special Case: C-Style array / Function as function argument
Since we know that c array will decay to pointer to its first element, and function will also decay to pointer.
What the compiler sees(using compiler insight):
As expected, the outcome is int * param
, and a function pointer.
However, when template argument is a reference:
The outcome is a reference (int (¶m)[3]
/ void (¶m)(int, double)
) to the array(or function) passed in!