lundi 20 avril 2015

Template function specialization for template class

Is it possible to write something like this in C++11/14?

#include <iostream>
#include <vector>

template <typename T>
size_t Get(const T& data);

template <typename T>
struct Data {
    std::vector<T> data;
};

template <>
template <typename T>
size_t Get<Data<T>>(const Data<T>& data) {
    return data.data.size();
}

int main() {
    std::cout << Get<>(Data<int>{}) << std::endl;  // expected output is 0
    return 0;
}

I know that I can just overload Get in the following manner:

template <typename T>
size_t Get(const Data<T>& data) {
    return data.data.size();
}

But it would be impossible for compiler to distinguish Get<Data<int>> from Get<Data<Data<int>>>.

Aucun commentaire:

Enregistrer un commentaire