Reputation: 1272
I think it's not possible but someone must know better...
template<typename T>
T Read() //T is int, char, etc
{
return read<T>();
}
template<typename T, Size>
std::array<T,Size> Read<std::array<T, Size>>()
{
return unique_read<T, Size>();
}
I guess as soon as I specify any template argument it's no longer a full specialization, and partial specialization is not allowed in functions
The only thing I could come up with is:
template<typename T>
struct _dummy
{
T Read() {
return T();
};
};
template<typename T, size_t Size>
struct _dummy<std::array<T, Size>>
{
using ArrayType = std::array<T, Size>;
ArrayType Read() {
return ArrayType();
};
};
Upvotes: 0
Views: 303
Reputation: 69882
The other way to do this is to defer to a function object:
template<class T>
struct reader_op {
T operator()() const {
// whatever needs to happen here
}
};
// partially specialise for array case
template<class T, size_t N>
struct reader_op<std::array<T, N>> {
std::array<T, N> operator()() const {
// whatever needs to happen here
}
};
// reader_op class type is now deduced from T
// when T is a std::array, our specialised function object will
// be used
template<typename T>
T Read() //T is int, char, etc
{
return reader_op<T>()();
}
Upvotes: 0
Reputation: 96810
You should use tag dispatching for this sort of job:
namespace detail {
template<class T>struct tag{};
template<class T>
T Read(tag<T>) {
return T{};
}
template<class T, std::size_t N>
std::array<T, N> Read(tag<std::array<T, N>>) {
return {1,2,3};
}
}
template<class T>
auto Read() {
return detail::Read(detail::tag<T>());
}
int main() {
Read<std::array<int,5>>();
}
Upvotes: 3