The following code works:
#include <iostream>
template<class T>
struct Test
{
T t;
template<class T, template<class U =3D T> class V>
Test<T>& foo(V<T>& f)
{
f.t =3D 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 =3D T> class V>
Test<T>& Test<T>::foo(V<T>& f)
{
f.t =3D 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=F6m
|
|
0
|
|
|
|
Reply
|
eriwik (500)
|
12/14/2006 11:56:34 AM |
|
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/
|
|
0
|
|
|
|
Reply
|
chattengau (616)
|
12/14/2006 12:03:36 PM
|
|
On Dec 14, 1:03 pm, "Steven T. Hatton" <chatten...@germania.sup> wrote:
> Erik Wikstr=F6m wrote:
> > The following code works:
>
> > #include <iostream>
>
> > template<class T>
> > struct Test
> > {
> > T t;
> > template<class T, template<class U =3D T> class V>
> > Test<T>& foo(V<T>& f)
> > {
> > f.t =3D 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 =3D T> class V>
> > Test<T>& Test<T>::foo(V<T>& f)
> > {
> > f.t =3D 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 =3D T part? I tried with "template<class T,
template<class> class V>" instead but with no luck.
--
Erik Wikstr=F6m
|
|
0
|
|
|
|
Reply
|
eriwik (500)
|
12/14/2006 12:19:51 PM
|
|
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/
|
|
0
|
|
|
|
Reply
|
chattengau (616)
|
12/14/2006 12:52:10 PM
|
|
Erik Wikstr=F6m wrote:
> The following code works:
>
> #include <iostream>
>
> template<class T>
> struct Test
> {
> T t;
> template<class T, template<class U =3D T> class V>
> Test<T>& foo(V<T>& f)
> {
> f.t =3D 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
|
|
0
|
|
|
|
Reply
|
greghe (676)
|
12/14/2006 1:00:46 PM
|
|
On Dec 14, 1:52 pm, "Steven T. Hatton" <chatten...@germania.sup> 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 =3D T> class V>
> Test<T>& foo(V<T>& f)
> {
> f.t =3D 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 =3D 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 =3D 5;
return *this;
}
int main() {
Test<int> t;
t.foo(t);
std::cout << t.t;
}
Once again, the "template<template<class U =3D 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=F6m
|
|
0
|
|
|
|
Reply
|
eriwik (500)
|
12/14/2006 1:04:38 PM
|
|
|
5 Replies
18 Views
(page loaded in 0.078 seconds)
Similiar Articles: Non-member operator overloading, linker complains - comp.lang.c++ ...... fix this, you'll need to forward declare the template operator-() (before the definition of class ... const member functions in classes derived from templates. - comp ... You ... class definition containing member of own type - comp.lang.c++ ...... of a template can only occur at namespace ... class definition containing member of own ... size ... member function, the type of this pointer ... const ... base class ... nitializing a static vector <> of integers (this static vectorclass definition containing member of own type - comp.lang.c++ ..... My_class* p ... Resources Network Get allocator (public member function ) Member types of template <class ... Static const integral data members can be initialized? - comp.lang ...... even when the template is instantiated on an int. > and if you do so without providing the out-of-class definition ... comp.lang ... const member functions in classes ... Static reflection - a base for runtime reflection? - comp.lang.c++ ...... through fields, member functions and types of the class e ... classes (imagine template that produces a class that ... class definition containing member of own type - comp ... Gradebook template - comp.databases.filemakerIs anyone aware of a (free?) gradebook template for fmp? ... Thus, many students enroll in one OR MORE classes, and ... From there you can use the Aggregate functions to gather ... Pointer to function Typedefs - comp.lang.c++.moderatedI have seen lot of definition like the one below in code ... Const constructor - comp.lang.c++.moderated Class Template Defining Typedef For Member Function ... old ... Const constructor - comp.lang.c++.moderated... something is to achieve is a separate ... Now, you can write a single function like this: template<class T ... explicit constructor ... const member functions in classes ... SOLUTION: compile time array size using type only - comp.lang.c++ ...... array size using type only ///// template class cx ... if it was, since you haven't provided a definition ... of course, to correspond to those of the member functions ... sizeof nonstatic member - comp.lang.c++.moderated... write > sizeof(A::a2) Try: template ... Base classes, 5. Virtual functions, 6. Non-static members of ... class definition containing member of own type - comp.lang.c++ ... Template member functions in template class with separate definitionTemplate member functions in template class with separate definition. C / C++ Forums on Bytes. Member Functions of Template Classes (C++)Note that just as with any template class member function, the definition of the class's constructor member function includes the template argument list twice. 7/18/2012 2:13:52 PM
|