C++2c(C++26)標準ライブラリstd::function_ref<R(ArgsTypes...)>クラステンプレートでは、先行宣言のみプライマリテンプレートが可変長引数テンプレート(variadic templates)となっている。技術的には単一のテンプレートパラメータで十分だが、将来拡張のために可変長テンプレートパラメータにしたとのこと。
本記事の内容はStackOverflowで見つけた質問と回答に基づく。
// [func.wrap.ref], non-owning wrapper template<class... S> class function_ref; // freestanding, not defined template<class R, class... ArgTypes> class function_ref<R(ArgTypes...) cv noexcept(noex)>;
提案文書P0792R14, Changelogより引用(下線部は強調)。
R9
P0792R14 function_ref: a type-erased callable reference
- Declare the main template as variadic for future extension;
- Allow declaring a variable of
function_refconstinit.
同様に、std::move_only_function(C++23)とstd::copyable_function(C++2c)のプライマリテンプレート宣言も可変長テンプレートパラメータを持つ。*1
一方、std::function(C++11)のプライマリテンプレート宣言は単一のテンプレートパラメータを持つ。
// [func.wrap.func], class template function template<class> class function; // not defined template<class R, class... ArgTypes> class function<R(ArgTypes...)>; // [func.wrap.move], move-only wrapper template<class... S> class move_only_function; // not defined template<class R, class... ArgTypes> class move_only_function<R(ArgTypes...) cv ref noexcept(noex)>; // [func.wrap.copy], copyable wrapper template<class... S> class copyable_function; // not defined template<class R, class... ArgTypes> class copyable_function<R(ArgTypes...) cv ref noexcept(noex)>;
ノート:std::functionクラステンプレートはC++2d以降で非推奨化(deprecated)される可能性があり*2、将来的な変更見込みはないだろう。
関連URL
- P0288R9 move_only_function
- P0792R14 function_ref: a type-erased callable reference
- (PDF) P2548R6 copyable_function
- std::functionと愉快な仲間たち ~move_only_function, copyable_function, function_ref~ #C++ - Qiita
*1:move_only_function 検討途中の提案文書 P0288R6 にて単一パラメータから可変長パラメータへと変更されている。https://github.com/cplusplus/papers/issues/400 でも詳細経緯は不明。