yohhoyの日記

技術的メモをしていきたい日記

非型テンプレートパラメータでのオーバーロード解決は行われない

関数テンプレートの非型テンプレートパラメータ(non-type template parameter)において、通常のC++オーバーロード解決規則は適用されない。沼に近よるべからず ('ω'乂)

template <bool N> void f() {}  // #1
template <int N>  void f() {}  // #2

f<0>();  // NG: オーバーロード解決は曖昧
f<1>();  // NG: オーバーロード解決は曖昧
f<false>();  // NG: オーバーロード解決は曖昧
f<true>();   // NG: オーバーロード解決は曖昧

f<2>();  // OK: #2を呼び出す
void g(bool);  // #1
void g(int);   // #2

g(0);  // OK: #2を呼び出す
g(1);  // OK: #2を呼び出す
g(false);  // OK: #1を呼び出す
g(true);   // OK: #1を呼び出す

g(2);  // OK: #2を呼び出す

You can declare them; you can't really use the bool one, because there's no mini-overload-resolution for template arguments; everything that's a valid converted constant expression of the template parameter's type is equally good. Regardless, if you actually see a negative impact from dummy parameters passed to control overload resolution, you should be filing bugs with your compiler vendor, not contorting your code (further).

https://stackoverflow.com/questions/34972679/#comment57675229_34972679

関連URL