Does this mimic perfectly a function template specialization?

Posted by zeroes00 on Stack Overflow See other posts from Stack Overflow or by zeroes00
Published on 2010-04-11T11:22:31Z Indexed on 2010/04/11 11:33 UTC
Read the original article Hit count: 366

Filed under:
|
|

Since the function template in the following code is a member of a class template, it can't be specialized without specializing the enclosing class.

But if the compiler's full optimizations are on (assume Visual Studio 2010), will the if-else-statement in the following code get optimized out? And if it does, wouldn't it mean that for all practical purposes this IS a function template specialization without any performance cost?

template<typename T>
struct Holder
{
    T   data;

    template<int Number>
    void saveReciprocalOf();
};

template<typename T>
template<int Number>
void Holder<T>::saveReciprocalOf()
{
    //Will this if-else-statement get completely optimized out
    if(Number == 0)     data = (T)0;
    else                data = (T)1 / Number;
}

//-----------------------------------
void main()
{
    Holder<float> holder;
    holder.saveReciprocalOf<2>();
    cout << holder.data << endl;
}

© Stack Overflow or respective owner

Related posts about function

Related posts about template