Predefined values for some values in a template function like with a #define macro (e.g. template)
The trick is to use a byte_repeater like here. 

template&lt;typename T, std::size_t count, int byte&gt;
struct byte_repeater;

template&lt;typename T, int byte&gt;
struct byte_repeater&lt;T, 1, byte&gt; {
    static constexpr T value = byte;
};

template&lt;typename T, std::size_t count, int byte&gt;
struct byte_repeater {
    static constexpr auto value = (byte_repeater&lt;T, count-1, byte&gt;::value &lt;&lt; CHAR_BIT) | byte;
};


Add a convenient function make_mask

template&lt;typename T, int mask&gt;
struct make_mask {
    static constexpr auto value = byte_repeater&lt;typename std::make_unsigned&lt;T&gt;::type, sizeof(T), mask&gt;::value;
};


And then the ostream&lt;&lt; operator becomes:

template&lt;typename T&gt;
std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const std::vector&lt;T&gt;&amp; obj)
{
    static constexpr auto width = sizeof(T) * 2;
    static constexpr auto mask = make_mask&lt;T,0xFF&gt;::value;
    os &lt;&lt; "[";
    std::for_each(obj.begin(), obj.end() - 1, [&amp;os](const auto&amp; elem) {
        os &lt;&lt; "0x" &lt;&lt; std::hex &lt;&lt; std::uppercase &lt;&lt; std::setw(width) &lt;&lt; std::setfill('0') &lt;&lt; (elem &amp; mask) &lt;&lt; ", ";
    });
    os &lt;&lt; "0x" &lt;&lt; std::hex &lt;&lt; std::uppercase &lt;&lt; std::setw(width) &lt;&lt; std::setfill('0') &lt;&lt; (obj.back() &amp; mask);
    os &lt;&lt; "]";
    return os;
}


LIVE DEMO