N
NKOBAYE027
FIRST POST
Hi All: I'm trying to write a simple specialization before moving on to
something a bit more complex - always a good idea in my case, at least. )
I'm trying to adapt the example from Stroustrup, 3rd ed., The C++
Programming Language p. 344
I'm using MSDev 6.0 in case that's an issue. Here's the source...
/* begin snippet */
/* specializations.hpp */
#include <functional>
using namespace std;
class foo
{
public:
int m_foo;
foo():m_foo(0){}
};
template<> bool less<foo>(const foo& afoo, const foo& bfoo)
{
return afoo.m_foo < bfoo.m_foo;
}
/* specializationstest.cpp */
#include "specializations.hpp"
int main()
{
foo f,g;
g.m_foo = 8;
bool done = less<foo>(f,g);
return done;
}
Compiling...
specializationstest.cpp
error C2935: 'less<class foo>' : template-class-id redefined as a global
function
error C2912: explicit specialization; 'bool __cdecl less<foo>(const class
foo &,const class foo &)' is not a function template
see declaration of 'less<class foo>'
error C2661: 'less<class foo>::less<class foo>' : no overloaded function
takes 2 parameters
Error executing cl.exe.
specializations.exe - 3 error(s), 0 warning(s)
/* end snippet */
Any help would be appreciated.
I'm trying to do this with the intent of specializing an iterator operator*
and & in the stuff I'm working on right now.
i.e.
something like
modified by OP:
had been glibly using foo as the template args for the lists
in the example code below - was 3 am, gimme a break )
this makes more sense
template<typename T>
class foo<T>
{
public:
list<T> foolist;
list<T*>p_foolist;
list<int> somestupidlist;
typedef list<T*>::iterator iterator;
}
template<> T& foo::iterator:perator*(){/*...*/}
where dereferencing the iterator will return an object reference, not a
pointer to the object which is what the list contains.
so, any suggestions here would be welcome as well...
And thanks to all those who replied to my prior questions...
regards,
L.
SECOND POST
Hi folks: Can anyone tell me what the difference is between the two
functions below? In particular why does the function that returns a
reference give me warnings about returning the address of a temporary
variable, whereas the prior function compiles without any errors or warnings
template<typename foo>
class fooset<foo>
{
...
list<foo> fooList;
typedef list<foo>::iterator iterator;
...
public:
iterator begin()
{
return fooList.begin();
}
/*
iterator& begin()
{
return fooList.begin();
}
*/
iterator
};
thanks,
and regards,
L.
Hi All: I'm trying to write a simple specialization before moving on to
something a bit more complex - always a good idea in my case, at least. )
I'm trying to adapt the example from Stroustrup, 3rd ed., The C++
Programming Language p. 344
I'm using MSDev 6.0 in case that's an issue. Here's the source...
/* begin snippet */
/* specializations.hpp */
#include <functional>
using namespace std;
class foo
{
public:
int m_foo;
foo():m_foo(0){}
};
template<> bool less<foo>(const foo& afoo, const foo& bfoo)
{
return afoo.m_foo < bfoo.m_foo;
}
/* specializationstest.cpp */
#include "specializations.hpp"
int main()
{
foo f,g;
g.m_foo = 8;
bool done = less<foo>(f,g);
return done;
}
Compiling...
specializationstest.cpp
error C2935: 'less<class foo>' : template-class-id redefined as a global
function
error C2912: explicit specialization; 'bool __cdecl less<foo>(const class
foo &,const class foo &)' is not a function template
see declaration of 'less<class foo>'
error C2661: 'less<class foo>::less<class foo>' : no overloaded function
takes 2 parameters
Error executing cl.exe.
specializations.exe - 3 error(s), 0 warning(s)
/* end snippet */
Any help would be appreciated.
I'm trying to do this with the intent of specializing an iterator operator*
and & in the stuff I'm working on right now.
i.e.
something like
modified by OP:
had been glibly using foo as the template args for the lists
in the example code below - was 3 am, gimme a break )
this makes more sense
template<typename T>
class foo<T>
{
public:
list<T> foolist;
list<T*>p_foolist;
list<int> somestupidlist;
typedef list<T*>::iterator iterator;
}
template<> T& foo::iterator:perator*(){/*...*/}
where dereferencing the iterator will return an object reference, not a
pointer to the object which is what the list contains.
so, any suggestions here would be welcome as well...
And thanks to all those who replied to my prior questions...
regards,
L.
SECOND POST
Hi folks: Can anyone tell me what the difference is between the two
functions below? In particular why does the function that returns a
reference give me warnings about returning the address of a temporary
variable, whereas the prior function compiles without any errors or warnings
template<typename foo>
class fooset<foo>
{
...
list<foo> fooList;
typedef list<foo>::iterator iterator;
...
public:
iterator begin()
{
return fooList.begin();
}
/*
iterator& begin()
{
return fooList.begin();
}
*/
iterator
};
thanks,
and regards,
L.