Tag dispatch versus static methods on partially specialised classes

后端 未结 4 1287
無奈伤痛
無奈伤痛 2020-12-07 08:54

Suppose I want to write a generic function void f(), which does one thing if T is a POD type and another thing if T is non-PO

4条回答
  •  再見小時候
    2020-12-07 09:32

    I would like tag dispatch because:

    • Easy to extend with new tags
    • Easy to use inheritance (example)
    • It is fairly common technique in generic programming

    It seems tricky to me to add third variant in second example. When you'll want to add, for example non-POD-of-PODs type you'll have to replace bool in template struct f2; with something other (int if you like =) ) and replace all struct f2 with struct f2. So that for me the second variant looks hardly extensible. Please correct me if I wrong.

提交回复
热议问题