lundi 29 juillet 2019

Specialized template accepting constructor parameter when only default constructor defined

So, I have this template class and its specialization.

    #include <iostream>

    using namespace std;

    template<bool> struct CompileTimeChecker{

       CompileTimeChecker(...) \\constructor, can accept any number of parameters;

    };

    template<> struct CompileTimeChecker<false> {};

Case 1:

In the main function I am defining a local class called ErrorA. When I create a temporary of CompileTimeChecker<false> with temporary object of ErrorA fed as an initializer, the compiler is not detecting any error.

int main()
{


    class ErrorA {};

    CompileTimeChecker<false>(ErrorA()); //Case 1;

    CompileTimeChecker<false>(int());    //Case 2;

    return 0;

}

Case 2:

Next I feed it with temporary object of type int, and suddenly the compiler recognizes the issue (there is no constructor that takes args in the specialized template CompileTimeChecker<false>)

main.cpp:30:36: error: no matching function for call to ‘CompileTimeChecker::CompileTimeChecker(int)’ CompileTimeChecker<false>(int());

Why does it not recognize the issue in case 1?

Aucun commentaire:

Enregistrer un commentaire