Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make C++ template type to default to previous template type

Is there any way, to default second template type to the type of the first template argument type, to remove the need of specifying the template types when not needed?

So, I have a method, that pops values from byte array:

    template<class TYPE1, class TYPE2>
    bool pop_value_from_array(TYPE2 &value)
    {
        if(get_array_size() >= sizeof(TYPE1))
        {
            value = static_cast<TYPE2>(*reinterpret_cast<const TYPE1*>(get_array_pointer()));
            remove_bytes_from_array(sizeof(TYPE1));
            return true;
        }
        return false;
    }

And then I can do

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    uint32_t size;
    if (!pop_value_from_array<uint16_t>(size))
    {
        // Error handling
    }
    size *= 2;
    uint16_t var2;
    if (!pop_value_from_array<uint16_t>(var2))
    {
        // Error handling
    }

If I declare another function:

    template<class TYPE>
    bool pop_value_from_array(TYPE &value)
    {
        if(get_array_size() >= sizeof(TYPE))
        {
            value = *reinterpret_cast<const TYPE*>(get_array_pointer());
            remove_bytes_from_array(sizeof(TYPE));
            return true;
        }
        return false;
    }

I can get rid of the template specification when not needed:

    uint32_t size;
    // Poping uint16_t from array, and storing to the uint32_t type, so there is no overflow in multiplication
    if (!pop_value_from_array<uint16_t>(size))
    {
        // Error handling
    }
    size *= 2;
    uint16_t var2;
    if (!pop_value_from_array(var2))
    {
        // Error handling
    }

So, I want to get rid of the second declaration of the same function

>Solution :

You might default TYPE1 to some special type (I use void here), and then check for that type:

template<class TYPE1 = void, class TYPE2>
bool pop_value_from_array(TYPE2 &value)
{
    using type = std::conditional_t<std::is_same_v<void, TYPE1>, TYPE2, TYPE1>;
    // Now use type.
    if(get_array_size() >= sizeof(type))
    {
        value = static_cast<TYPE2>(*reinterpret_cast<const type*>(get_array_pointer()));
        remove_bytes_from_array(sizeof(type));
        return true;
    }
    return false;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading