specialization or overload

Discussion in 'C++' started by nguillot, Oct 4, 2011.

  1. nguillot

    nguillot Guest

    Hello.

    I would like to specialize a templated function for the type
    std::vector<U>, U being a typename.
    I think it is not possible.

    However I read on forum we can (I describe just after how) but I guess
    this is not specialization but overload.

    I’ve got 2 questions:
    1) Is it specialization or overload?
    2) In fact, if it works, I don’t really care the answer of this
    question 1 (execpt for my knowledge): does the standard assure it
    works (resolution order) ?

    Now the stuff:

    Let’s have the template function:

    template <typename T>
    void f(const T& p_value)
    {
    cout << "generic" << endl;
    }

    The specialization (for bool for instance) is

    template <>
    void f<bool>(const bool& p_value)
    {
    cout << "bool" << endl;
    }

    The "specialization" for vector<U> is (or is it an overload?)

    template <typename U>
    void f(const std::vector<U> & p_value)
    {
    cout << "vector" << endl;
    }

    And indeed if I do :

    int i;
    vector<double> v;
    bool b;

    f(i);
    f(v);
    f(b);

    I get:

    generic
    vector
    bool

    So, is it really specialization?
    If no, am I sure f(v) will always calls this "specialization?

    Thanks in advance.
    nguillot, Oct 4, 2011
    #1
    1. Advertising

  2. On 10/4/2011 1:10 PM, nguillot wrote:
    > I would like to specialize a templated function for the type
    > std::vector<U>, U being a typename.
    > I think it is not possible.
    >
    > However I read on forum we can (I describe just after how) but I guess
    > this is not specialization but overload.
    >
    > I’ve got 2 questions:
    > 1) Is it specialization or overload?
    > 2) In fact, if it works, I don’t really care the answer of this
    > question 1 (execpt for my knowledge): does the standard assure it
    > works (resolution order) ?
    >
    > Now the stuff:
    >
    > Let’s have the template function:
    >
    > template<typename T>
    > void f(const T& p_value)
    > {
    > cout<< "generic"<< endl;
    > }
    >
    > The specialization (for bool for instance) is
    >
    > template<>
    > void f<bool>(const bool& p_value)
    > {
    > cout<< "bool"<< endl;
    > }
    >
    > The "specialization" for vector<U> is (or is it an overload?)
    >
    > template<typename U>
    > void f(const std::vector<U> & p_value)
    > {
    > cout<< "vector"<< endl;
    > }
    >
    > And indeed if I do :
    >
    > int i;
    > vector<double> v;
    > bool b;
    >
    > f(i);
    > f(v);
    > f(b);
    >
    > I get:
    >
    > generic
    > vector
    > bool
    >
    > So, is it really specialization?
    > If no, am I sure f(v) will always calls this "specialization?


    Here is how you can distinguish between an overload and a
    specialization. An overload has the same name as the other function and
    the specialization would have a different name, if the template
    argument is/are applied.

    In your case the original function that you defined (with the const T&
    argument) would have the name 'f<char>' if you substitute 'T' with
    'char', right? Now, the one that has the vector as its argument, the
    one with the template argument named 'U', would be named 'f<char>' if
    you substitute 'U' with 'char', correct? So, the two names are the
    same, but the functions are different, yes? So, it's an overload.

    Here is another way of looking at it:
    Since the 'vector' would otherwise be a partial specialization (since
    it's a template in itself), and there are no partial specializations of
    function templates, it can't be a specialization. It's an overload.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 4, 2011
    #2
    1. Advertising

  3. nguillot

    Noah Roberts Guest

    On Oct 4, 10:10 am, nguillot <> wrote:
    > Hello.
    >
    > I would like to specialize a templated function for the type
    > std::vector<U>, U being a typename.
    > I think it is not possible.
    >
    > However I read on forum we can (I describe just after how) but I guess
    > this is not specialization but overload.
    >
    > I’ve got 2 questions:
    > 1)      Is it specialization or overload?
    > 2)      In fact, if it works, I don’t really care the answer of this
    > question 1 (execpt for my knowledge): does the standard assure it
    > works (resolution order) ?


    It will work in name resolution if the overload is within current
    scope or within scope of the type that it is an overload for.

    The main difference between doing a specialization and an overload is
    that if you're going to explicitly supply the template argument you
    can't use an overload. Otherwise you SHOULD use the overload.
    >
    > Now the stuff:
    >
    > Let’s have the template function:
    >
    > template <typename T>
    > void f(const T& p_value)
    > {
    >     cout << "generic" << endl;
    >
    > }
    >
    > The specialization (for bool for instance) is
    >
    > template <>
    > void f<bool>(const bool& p_value)
    > {
    >     cout << "bool" << endl;
    >
    > }
    >
    > The "specialization" for vector<U> is (or is it an overload?)
    >
    > template <typename U>
    > void f(const std::vector<U> & p_value)
    > {
    >     cout << "vector" << endl;
    >
    > }
    >
    > And indeed if I do :
    >
    >     int i;
    >     vector<double> v;
    >     bool b;
    >
    >     f(i);
    >     f(v);
    >     f(b);
    >
    > I get:
    >
    > generic
    > vector
    > bool
    >
    > So, is it really specialization?
    > If no, am I sure f(v) will always calls this "specialization?


    It is an overload and with that syntax it will get called so long as
    it is within the searchable name scope. Name resolution rules are
    complicated, especially how and when ADT applies. You'll need to
    familiarize yourself with them.

    You probably noticed that functions cannot be partially specialized.
    You can't do this:

    template < typename U >
    void f<vector<U>>(vector<U> const&) {}

    If you need partial specialization for a function you have to use an
    interim class:

    template < typename T >
    struct f_impl
    {
    static void call(T const&) {}
    };

    template < typename T >
    void f(T const& t) { f_impl<T>::call(t); }

    You can then partially specialize f_impl.

    Again, this trick is needed only when you need to call the function
    with explicitly specified template parameters. If you're using
    deduction then you probably want to provide overloads instead of
    specializations, partial or otherwise.
    Noah Roberts, Oct 4, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eph0nk

    Error: Overload resolution failed

    Eph0nk, Oct 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,851
    Eph0nk
    Oct 23, 2003
  2. MattB

    Overload

    MattB, Dec 22, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    537
    MattB
    Dec 23, 2003
  3. Piotre Ugrumov
    Replies:
    3
    Views:
    362
    Nick Hounsome
    Jan 25, 2004
  4. Joseph Turian
    Replies:
    2
    Views:
    458
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    240
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page