473,698 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

repost: template function specialization & basic question...

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. :o)

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;
}

/* specializations test.cpp */
#include "specialization s.hpp"

int main()
{
foo f,g;
g.m_foo = 8;

bool done = less<foo>(f,g);

return done;
}

Compiling...
specializations test.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<clas s 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 :o)
this makes more sense

template<typena me T>
class foo<T>
{
public:
list<T> foolist;
list<T*>p_fooli st;
list<int> somestupidlist;
typedef list<T*>::itera tor iterator;
}

template<> T& foo::iterator:: operator*(){/*...*/}

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<typena me foo>
class fooset<foo>
{
...
list<foo> fooList;
typedef list<foo>::iter ator iterator;
...

public:

iterator begin()
{
return fooList.begin() ;
}

/*
iterator& begin()
{
return fooList.begin() ;
}
*/
iterator
};

thanks,
and regards,
L.
Jul 22 '05 #1
6 1770
"NKOBAYE027 " <NK********@Rog ers.Com> wrote in
news:Y9******** *************@t wister01.bloor. is.net.cable.ro gers.com:
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. :o)

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...

(...)
template<> bool less<foo>(const foo& afoo, const foo& bfoo)
{
return afoo.m_foo < bfoo.m_foo;
}


1. std::less is not a function, but a functor. It provides an overloaded
operator(), so it can be 'invoked' like a function.

2. Function templates cannot be specialized.

3. Why don't you write an operator < overload for your foo class?

Cheers,
bartek
Jul 22 '05 #2
> 1. std::less is not a function, but a functor. It provides an overloaded
operator(), so it can be 'invoked' like a function.

2. Function templates cannot be specialized.
Function templates can be specialized. They cannot be
_partially_spec ialized_. (OTOH, MSVC6 doesn't support partial template
specialization anyway.)

3. Why don't you write an operator < overload for your foo class?

Cheers,
bartek

Jul 22 '05 #3
"Adam H. Peterson" <ah**@email.byu .edu> wrote in
news:c1******** ***@acs2.byu.ed u:
Function templates can be specialized. They cannot be
_partially_spec ialized_. (OTOH, MSVC6 doesn't support partial
template specialization anyway.)


Oops, of course right. Sorry for the confusion.

Cheers,
bartek
Jul 22 '05 #4
Thanks bartek.

The reason I don't overload the < is because I'm not aiming to do that. This
was a first attempt at specializing a function - and I failed miserably. :o)
I see now that I was missing the original definition of the template
function 'less' provided in the text (on the same page, no less...npi). So,
I guess, what I should do is simply ask if specializing a list<class
T*>::iterator is possible such that dereferencing the iterator will return
the object pointed to by T* instead of T*. If so how?

Sorry for the confusion - it's all mine!

I've since tried the specialized version and it works when I appropriately
define it and the original, but would still greatly appreciate any input on
the iterator issue mentioned above.

regards,
L.
Jul 22 '05 #5
NKOBAYE027 wrote:
Thanks bartek.

The reason I don't overload the < is because I'm not aiming to do that. This
was a first attempt at specializing a function - and I failed miserably. :o)
I see now that I was missing the original definition of the template
function 'less' provided in the text (on the same page, no less...npi). So,
I guess, what I should do is simply ask if specializing a list<class
T*>::iterator is possible such that dereferencing the iterator will return
the object pointed to by T* instead of T*. If so how?


I don't think you can do this with template specialization. In my
experience (and I think in all cases) when you specialize a function or
class, you can't alter the signatures of the functions -- what
parameters they take, their constness, their return type.

It looks like you're trying to create something akin to a polymorphic
container. You can probably search on that -- there's been lots of
discussion, research, etc. on that topic.

Personally, I would start to solve such a problem by using composition
with a standard list<>. Something like:
#include <list>

template<typena me T>
class ptr_list {
public:
class iterator;
// Yada yada
private:
std::list<T*> data;
};

template<typena me T>
class ptr_list<T>::it erator {
public:
T &operator*() const {return **it;}
// Yada yada
private:
std::list<T*>:: iterator it;
};
It would take some work to get the full iterator interface working, but
you may not need all of that depending on your usage.

Adam H. Peterson
Jul 22 '05 #6
Thanks, Adam - that was exactly what I was looking for. Your pointer about
specializations not being able to change function signatures is what I
needed. So, I do need to reinvent the wheel, so-to-speak, for the iterator
class. I can see no other way than the one you mentioned - and I'd already
considered that but was hoping for an easier way out...Ugh...c'e st la vie,
non?

regards,
L.

"Adam H. Peterson" <ah**@email.byu .edu> wrote in message
news:c1******** ***@acs2.byu.ed u...
NKOBAYE027 wrote:
Thanks bartek.

The reason I don't overload the < is because I'm not aiming to do that. This was a first attempt at specializing a function - and I failed miserably. :o) I see now that I was missing the original definition of the template
function 'less' provided in the text (on the same page, no less...npi). So, I guess, what I should do is simply ask if specializing a list<class
T*>::iterator is possible such that dereferencing the iterator will return the object pointed to by T* instead of T*. If so how?


I don't think you can do this with template specialization. In my
experience (and I think in all cases) when you specialize a function or
class, you can't alter the signatures of the functions -- what
parameters they take, their constness, their return type.

It looks like you're trying to create something akin to a polymorphic
container. You can probably search on that -- there's been lots of
discussion, research, etc. on that topic.

Personally, I would start to solve such a problem by using composition
with a standard list<>. Something like:
#include <list>

template<typena me T>
class ptr_list {
public:
class iterator;
// Yada yada
private:
std::list<T*> data;
};

template<typena me T>
class ptr_list<T>::it erator {
public:
T &operator*() const {return **it;}
// Yada yada
private:
std::list<T*>:: iterator it;
};
It would take some work to get the full iterator interface working, but
you may not need all of that depending on your usage.

Adam H. Peterson

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
6855
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because that is returned by equal_range()).
2
2538
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the template for your type. However, this requires you to copy the whole template, and change the method, which leads to code duplication... If there's only 1 template parameter, one can specialize the method
6
2015
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit instantiation of foo:
7
1837
by: Siemel Naran | last post by:
Hi. I have a function template <class InputIter, class OutputIter> void f(InputIter begin, InputIter end, OutputIter result); With c of type char* and cc of type const char*, the code f(c,c,cc) calls f<char*, const char *>, which is fine. But f(c,c,c) calls a new instantiation f<char*,char*> whereas I'd like it to call f<const char*,char*>.
1
2266
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual ~A() ;
9
2776
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second assign() function really a specialization of the first assign() or is it an assign() overload? Thank you. -- Marek
2
7693
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
4
3012
by: David Sanders | last post by:
Hi, I have a class with an integer template parameter, taking values 1, 2 or 3, and a function 'calc' in that class which performs calculations. Some calculations need only be performed if the template parameter is 2 or 3; for efficiency, I do not wish to perform the calculations if the template parameter is 1. I currently do this as follows:
5
3775
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static void UseMap (const std::string &key, int value)
0
8672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4361
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3038
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2323
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.