Я бы хотел, чтобы тип вариационного шаблона был идентичен типу более раннего заданного шаблона. В приведенном ниже примере я хотел бы, чтобы T и U были одного типа.Использовать вариационный шаблон определенного типа
#include <iostream>
#include <string>
template<class T>
struct Foo {
Foo(T val) {
std::cout << "Called single argument ctor" << std::endl;
// [...]
}
// How to enforce U to be the same type as T?
template<class... U>
Foo(T first, U... vals) {
std::cout << "Called multiple argument ctor" << std::endl;
// [...]
}
};
int main() {
// Should work as expected.
Foo<int> single(1);
// Should work as expected.
Foo<int> multiple(1, 2, 3, 4, 5);
// Should't work (but works right now). The strings are not integers.
Foo<int> mixedtype(1, "a", "b", "c");
// Also shouldn't work. (doesn't work right now, so that is good)
Foo<int> alsomixedtype(1, 1, "b", "c");
}
связанныхсоздоровья [Метод вариационного шаблона для принятия заданного количества парных чисел?] (Http://stackoverflow.com/questions/30179181) – Ruslan