Boost logo

Boost Users :

Subject: Re: [Boost-users] Function template specialization
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-12-18 09:41:38


Surya Kiran Gullapalli wrote:
> Hello all,
> The following question may not be related to boost, but if a solution can be
> found with boost or otherwise would be great.
>
> I've a base class with a function template.
>
> I derive from base class and try to have a specialization for the function
> template in derived class
>
> I did something like this.
>
> ////////////////////////// Header file begin //////////////////////////
> class Base
> {
> ..
> template <typename T>
> fun (T arg) { ... }
>
> };
>
> class Derived : public Base
> {
> ...
> } ;
>
> template <>
> Derived::fun(int arg);
>
> ///////////////////////// Header file end ////////////////////////////
>
> and in .cpp file I've provided implementation for the template
> specialization.
>
> This works fine with MSVC 8.0 and g++ complains about lack of function
> declaration fun in Derived class.
>
> I do not know which compiler is behaving correctly. Any help in this is
> greatly appreciated.
>

You can't do this. I don't think that it even does what
you want with msvc. (Base().fun(1) will call the specialization)
In general function templates should be overloaded rather than
specialized.

class Base
{
    template<typename T>
    void fun(T arg);
};

class Derived : public Base
{
    using Base::fun;
    void fun(int);
};

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net