dimanche 28 avril 2019

Why non deductible template parameters is problematic?

Trying to compile this piece of code results in an error.

template <typename T>
struct MyClass;

template <typename T, std::size_t sz>
struct MyClass<T> {
    static void print() {
        std::cout << "Partially specialized" << std::endl;
    }
};

error: template parameters not deducible in partial specialization

Trying to make it possible to deduce sz solves the problem. For example:

template <typename T, std::size_t sz>
struct MyClass<T[sz]> {
    static void print() {
        std::cout << "Partially specialized" << std::endl;
    }
};

My question is, how does the compiler interprets such a program and what's the rational behind not allowing to have the first structure?

Further clarification: I am thinking that if the inital code was compilable, we could use it in a manner such as: MyClass<int, 4>::print().

Aucun commentaire:

Enregistrer un commentaire