Template member functions in template class with separate definition

Discussion in 'C++' started by =?iso-8859-1?q?Erik_Wikstr=F6m?=, Dec 14, 2006.

  1. The following code works:

    #include <iostream>

    template<class T>
    struct Test
    {
    T t;
    template<class T, template<class U = T> class V>
    Test<T>& foo(V<T>& f)
    {
    f.t = 5;
    return *this;
    }
    };

    int main() {
    Test<int> t;
    t.foo(t);
    std::cout << t.t;

    }

    However, if I try to move the definition of the foo()-function outside
    of the declaration like so:

    template<typename T, template<typename U = T> class V>
    Test<T>& Test<T>::foo(V<T>& f)
    {
    f.t = 5;
    return *this;
    }

    It does not compile (VC++8.0) with the following error-message:

    test.cpp(24) : error C2244: 'Test<T>::foo' : unable to match function
    definition to an existing declaration
    definition
    'Test<T> &Test<T>::foo(V<T> &)'
    existing declarations
    'Test<T> &Test<T>::foo(V<T> &)'

    Anyone have an idea of how to make this work?

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Dec 14, 2006
    #1
    1. Advertising

  2. Erik Wikström wrote:

    > The following code works:
    >
    > #include <iostream>
    >
    > template<class T>
    > struct Test
    > {
    > T t;
    > template<class T, template<class U = T> class V>
    > Test<T>& foo(V<T>& f)
    > {
    > f.t = 5;
    > return *this;
    > }
    > };
    >
    > int main() {
    > Test<int> t;
    > t.foo(t);
    > std::cout << t.t;
    >
    > }
    >
    > However, if I try to move the definition of the foo()-function outside
    > of the declaration like so:
    >
    > template<typename T, template<typename U = T> class V>
    > Test<T>& Test<T>::foo(V<T>& f)
    > {
    > f.t = 5;
    > return *this;
    > }
    >
    > It does not compile (VC++8.0) with the following error-message:
    >
    > test.cpp(24) : error C2244: 'Test<T>::foo' : unable to match function
    > definition to an existing declaration
    > definition
    > 'Test<T> &Test<T>::foo(V<T> &)'
    > existing declarations
    > 'Test<T> &Test<T>::foo(V<T> &)'
    >
    > Anyone have an idea of how to make this work?


    The default argument in the function _definition_ looks wrong to me.

    --
    NOUN:1. Money or property bequeathed to another by will. 2. Something handed
    down from an ancestor or a predecessor or from the past: a legacy of
    religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
    from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
     
    Steven T. Hatton, Dec 14, 2006
    #2
    1. Advertising

  3. On Dec 14, 1:03 pm, "Steven T. Hatton" <> wrote:
    > Erik Wikström wrote:
    > > The following code works:

    >
    > > #include <iostream>

    >
    > > template<class T>
    > > struct Test
    > > {
    > > T t;
    > > template<class T, template<class U = T> class V>
    > > Test<T>& foo(V<T>& f)
    > > {
    > > f.t = 5;
    > > return *this;
    > > }
    > > };

    >
    > > int main() {
    > > Test<int> t;
    > > t.foo(t);
    > > std::cout << t.t;

    >
    > > }

    >
    > > However, if I try to move the definition of the foo()-function outside
    > > of the declaration like so:

    >
    > > template<typename T, template<typename U = T> class V>
    > > Test<T>& Test<T>::foo(V<T>& f)
    > > {
    > > f.t = 5;
    > > return *this;
    > > }

    >
    > > It does not compile (VC++8.0) with the following error-message:

    >
    > > test.cpp(24) : error C2244: 'Test<T>::foo' : unable to match function
    > > definition to an existing declaration
    > > definition
    > > 'Test<T> &Test<T>::foo(V<T> &)'
    > > existing declarations
    > > 'Test<T> &Test<T>::foo(V<T> &)'

    >
    > > Anyone have an idea of how to make this work?

    >
    >The default argument in the function _definition_ looks wrong to me.


    You mean the U = T part? I tried with "template<class T,
    template<class> class V>" instead but with no luck.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Dec 14, 2006
    #3
  4. Erik Wikström wrote:

    > #include <iostream>
    >
    > template<class T>
    > struct Test
    > {
    > T t;
    > template<class T, template<class U = T> class V>
    > Test<T>& foo(V<T>& f)
    > {
    > f.t = 5;
    > return *this;
    > }
    > };
    >
    > int main() {
    > Test<int> t;
    > t.foo(t);
    > std::cout << t.t;
    >
    > }
    >
    > However, if I try to move the definition of the foo()-function outside
    > of the declaration like so:
    >
    > template<typename T, template<typename U = T> class V>
    > Test<T>& Test<T>::foo(V<T>& f)
    > {
    > f.t = 5;
    > return *this;
    > }

    The first form doesn't compile for me with GCC 4.0.2. Have I got it right?

    //templ.cpp
    #include <iostream>

    template<class T>
    struct Test
    {
    T t;
    template<class T, template<class U = T> class V>
    Test<T>& foo(V<T>& f)
    {
    f.t = 5;
    return *this;
    }
    };

    int main() {
    Test<int> t;
    t.foo(t);
    std::cout << t.t;
    }
    //---------------EOF-----------------

    g++ -o templ templ.cpp
    templ.cpp:7: error: declaration of ?class T?
    templ.cpp:3: error: shadows template parm ?class T?

    Compilation exited abnormally with code 1 at Thu Dec 14 07:47:26

    --
    NOUN:1. Money or property bequeathed to another by will. 2. Something handed
    down from an ancestor or a predecessor or from the past: a legacy of
    religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
    from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
     
    Steven T. Hatton, Dec 14, 2006
    #4
  5. =?iso-8859-1?q?Erik_Wikstr=F6m?=

    Greg Guest

    Erik Wikström wrote:
    > The following code works:
    >
    > #include <iostream>
    >
    > template<class T>
    > struct Test
    > {
    > T t;
    > template<class T, template<class U = T> class V>
    > Test<T>& foo(V<T>& f)
    > {
    > f.t = 5;
    > return *this;
    > }
    > };
    >
    > int main() {
    > Test<int> t;
    > t.foo(t);
    > std::cout << t.t;
    >
    > }


    The program is using "T" as the name of two separate type parameters,
    So I would suggest giving each parameter a distinct name, maybe T1 and
    T2, so that the compiler can tell them part.

    Greg
     
    Greg, Dec 14, 2006
    #5
  6. On Dec 14, 1:52 pm, "Steven T. Hatton" <> wrote:
    >
    >The first form doesn't compile for me with GCC 4.0.2. Have I got it right?
    >
    > //templ.cpp
    > #include <iostream>
    >
    > template<class T>
    > struct Test
    > {
    > T t;
    > template<class T, template<class U = T> class V>
    > Test<T>& foo(V<T>& f)
    > {
    > f.t = 5;
    > return *this;
    > }
    >
    > };int main() {
    > Test<int> t;
    > t.foo(t);
    > std::cout << t.t;}//---------------EOF-----------------
    >
    > g++ -o templ templ.cpp
    > templ.cpp:7: error: declaration of ?class T?
    > templ.cpp:3: error: shadows template parm ?class T?


    Yes, that looks right, however I finally managed to find a working
    solution:

    #include <iostream>

    template<class T>
    struct Test
    {
    T t;
    template<template<class U = T> class V>
    Test<T>& foo(V<T>& f);
    };

    template<class T>
    template<template<class> class V>
    Test<T>& Test<T>::foo(V<T>& f)
    {
    f.t = 5;
    return *this;
    }

    int main() {
    Test<int> t;
    t.foo(t);
    std::cout << t.t;

    }

    Once again, the "template<template<class U = T> class V>" can be
    reduced to "template<template<class> class V>" but I think that it
    documents the intention that the type that V is parametrized by is the
    same as that of Test.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Dec 14, 2006
    #6
    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. BigMan
    Replies:
    3
    Views:
    1,466
    BigMan
    Apr 22, 2005
  2. Hicham Mouline
    Replies:
    0
    Views:
    348
    Hicham Mouline
    Nov 11, 2008
  3. Hicham Mouline
    Replies:
    0
    Views:
    454
    Hicham Mouline
    Apr 23, 2009
  4. Hicham Mouline
    Replies:
    1
    Views:
    440
    Michael DOUBEZ
    Apr 24, 2009
  5. Immortal Nephi
    Replies:
    12
    Views:
    918
    Paul Bibbings
    Jul 30, 2010
Loading...

Share This Page