dimanche 17 novembre 2019

Could someone explain me the following template code?

I am new to template programming and I intend to use the solution here to make sure that the types used have an operator defined. But I would like to understand this code. I looked up information of cppreference but I am more confused on how this works.

How to check whether operator== exists?

Unfortunately it is quite cryptic to me and thought would ask for the meaning and reasons for some of the things in the code.

{
    struct No {};
    template <typename T, typename Arg>
    No operator==(const T&, const Arg&);

    /* Why are there two definitions of structures? Is this needed at all? Is
     * there a simpler version of this whole code snippet?
     */
    template <typename T, typename Arg = T>
    struct EqualExists {
        /* Why does this have to be a enum? What effect does this have? */
        enum {
            /* What happens if == sign is not defined for T? What would the
             * below comparison return when true/false?
             *
             * What is the need for No here? Why is it needed and how will
             * comparing it with the return type of == comparison be true ever?
             * How can the return type of == comparison and No structure be the same?
             */
            value = !std::is_same<decltype(*(T*)(0) == *(Arg*)(0)), No>::value
        };
    };
}

Could someone link this to the original question? Hope this helps someone new to cpp to understand this as well.

Thank you in advance :)

Aucun commentaire:

Enregistrer un commentaire