Consider the two partial specializations below:
#include <iostream>
#include <vector>
#include <type_traits>
template <typename, typename...> struct A;
template <typename... Ts>
struct A<int, Ts...> {
void foo (int a) const {std::cout << a << '\n';}
void operator()(const std::vector<int>& v) const {std::cout << v.size() << '\n';}
};
template <typename... Ts>
struct A<char, Ts...> {
void foo (char a) const {std::cout << a << '\n';}
void operator()(const std::vector<char>& v) const {std::cout << v.size() << '\n';}
};
int main() {
A<int, long, double> a;
A<char, float, bool, short> b;
a.foo(5); // 5
b.foo('!'); // !
a({1,2,3}); // 3
b({1,2,3}); // 3
}
How to write the two specializations just once?
template <typename T, typename... Ts>
struct A<T, Ts...> {
static_assert (std::is_same<T,int>::value || std::is_same<T,char>::value, "Error");
void foo (T a) const {std::cout << a << '\n';}
void operator()(const std::vector<T>& v) const {std::cout << v.size() << '\n';}
};
doesn't work because it doesn't specialize anything, and I can't place class = std::enable_if<std::is_same<T,int>::value || std::is_same<T,char>::value, T>::type
anywhere because a default argument cannot go after a pack. The above specializations shall only be for int and char. Any other type will some other general definition for the class.
Aucun commentaire:
Enregistrer un commentaire