Figure 3: automation_vector constructor and attach functions

template <class T>
automation_vector<T>::automation_vector(VARIANT &vSource, TCreateMode Mode) 
    throw(std::invalid_argument, std::runtime_error)
    : base(0, myVARENUM())
{
    if (Mode == COPY)
    {
        // Make a copy and attach to it
        CComVariant v(vSource);
        attach(v);
    }
    else
        // Attach directly to the source
        attach(vSource);
}

...

void automation_vector_base::attach(VARIANT &v) throw() 
{
        clear();
        std::swap(static_cast<VARIANT &>(m_Value), v);
}
     
...

template <class T> 
void automation_vector<T>::attach(VARIANT &vSource) 
{
    ...
    const VARENUM vt = VARENUM(vSource.vt & VT_TYPEMASK);
    automation_vector Temp;
    if (vt == myVARENUM())
    {
        // Great, types match 
        com_enforce(::SafeArrayLock(&Array));
        from_automation(Array, static_cast<T *>(0));
        Temp.base::attach(vSource);
    }
    else
    {
        // Types don't match, we'll have to do a conversion
        // Convert each element one by one ...
        Temp.attach([Converted array]);
    }
    swap(Temp); // swap Temp with variable in this
                // automation_vector
}