呼び出し元が独自の適切な割り当て方法を指定できるテンプレートを作成しようとしていますが、バリデーショナルテンプレート引数を渡す際に問題があります。C++のテンプレート関数のエイリアスを可変のテンプレート引数として
引数を渡さないと、すべて正常に動作します。しかし、1つまたは複数の引数を渡すと、「関数呼び出しに引数が多すぎます」というコンパイルエラーが発生します。
私は間違っていますか?
#include <cstdio>
#include <memory>
template <typename T, typename... Args>
using allocator = std::unique_ptr<T>(Args...);
template <typename T, allocator<T> A, typename... Args>
std::unique_ptr<T> get(Args... args) {
return A(args...);
}
int main() {
auto up1 = get<int, std::make_unique<int>>(); // Works
auto up2 = get<int, std::make_unique<int>>(1); // Too many arguments
// expected 0, have 1
printf("%d\n", *up1);
printf("%d\n", *up2);
}
[この](http://coliru.stacked-crooked.com/a/cd68dec6691d5323)_works_が、これは本当にあなたが欲しいインタフェースですか..?これは私にとって[XY問題](http://meta.stackexchange.com/a/66378/166663)のようです。 – ildjarn
私はインターフェースを変更するためにリファクタリングするかもしれませんが、私はまだ基本的な問題を理解することに興味があります。この場合、variary引数がテンプレートエイリアスで機能しないのはなぜですか? –
'allocator'は、 'std :: unique_ptr (*)()'に調整された 'std :: unique_ptr ()'の 'allocator 'です。 –