473,699 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(Simple?) template problem

I'm having a bit of a template-problem, and I was wondering if you
guru's know what I'm doing wrong here...

I want to create a Vertex class that supports a toPoint() method. Now,
in my case there are 2 ways of calculating toPoint(), and I want to be
able to choose (at compile time) this method in a flexible way.
Therefore, I want to template this Vertex class.
For example, when I create a Vertex<fixed_ta g> I want the 'fixed'
toPoint()-method to be used, whereas when I create a
Vertex<interval _tag>, it should use the 'interval' variant.

What I've done so far is this:

//--------------------------
struct fixed_tag{};
struct interval_tag{};

template<class S>
class Vertex{
//
//Member-vars, constructor e.d.
//

template<fixed_ tag>
Point toPoint() const;

template<interv al_tag>
Point toPoint() const;

};

//Implementation

template<fixed_ tag>
Point Vertex::toPoint () const{
//Calculate and return fixed point
}

template<interv al_tag>
Point Vertex::toPoint () const{
//Calculate and return interval point
}

//---------------------------

Needless to say, the above code doesn't compile properly (VS.NET), and
I've tried quite a few variants on this, but I just can't seem to get
it *right*...

Who can tell me what I'm doing wrong here?
Thanks.
Jul 22 '05 #1
7 1591


Leon ha escrito:
I'm having a bit of a template-problem, and I was wondering if you
guru's know what I'm doing wrong here...

I want to create a Vertex class that supports a toPoint() method. Now,
in my case there are 2 ways of calculating toPoint(), and I want to be
able to choose (at compile time) this method in a flexible way.
Therefore, I want to template this Vertex class.
For example, when I create a Vertex<fixed_ta g> I want the 'fixed'
toPoint()-method to be used, whereas when I create a
Vertex<interval _tag>, it should use the 'interval' variant.

What I've done so far is this:


Seems to me like you're trying to specialize Vertex::toPoint ,
but C++ does not allow to specialize a single memfun. Several
alternatives come to mind:

1. Specialize Vertex (the whole class) for both fixed_tag and
interval_tag. This forces you to duplicate all the common code.
Not good.
2. Factor the common code into a base class (say VertexBase) and
then proceed as in 1.
3. If you don't envision future variants of toPoint, the following is
a common idiom to do a compile-time selection of the implementation
of a memfun:

struct Point{};

struct fixed_tag{};
struct interval_tag{};

template<class S>
class Vertex{
public:
//
//Member-vars, constructor e.d.
//

Point toPoint() const
{
return toPoint(S()); // forwards to the appropriate impl
}

private:
Point toPoint(const fixed_tag&) const;
Point toPoint(const interval_tag&) const;
};

//Implementation

template<class S>
Point Vertex<S>::toPo int(const fixed_tag&) const{
//Calculate and return fixed point
return Point();
}

template<class S>
Point Vertex<S>::toPo int(const interval_tag&) const{
//Calculate and return interval point
return Point();
}

int main()
{
Vertex<fixed_ta g> v;
v.toPoint();

Vertex<interval _tag> v2;
v2.toPoint();
}

Hope this helps,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo

Jul 22 '05 #2
Leon wrote in news:8d******** *************** ***@posting.goo gle.com in
comp.lang.c++:
I want to create a Vertex class that supports a toPoint() method. Now,
in my case there are 2 ways of calculating toPoint(), and I want to be
able to choose (at compile time) this method in a flexible way.
Therefore, I want to template this Vertex class.
For example, when I create a Vertex<fixed_ta g> I want the 'fixed'
toPoint()-method to be used, whereas when I create a
Vertex<interval _tag>, it should use the 'interval' variant.


/* wherever possible post compilable code
*/
struct Point {};

struct fixed_tag{};
struct interval_tag{};

template<class S>
class Vertex{
//
//Member-vars, constructor e.d.
//

public:

Point toPoint() const;
};

/* Provide explicit specialization' s for interval_tag and fixed_tag
*/

template<>
Point Vertex< fixed_tag >::toPoint() const
{
//Calculate and return fixed point
return Point();
}

template<>
Point Vertex< interval_tag >::toPoint() const
{
//Calculate and return interval point
return Point();
}
/* Test
*/

int main()
{
Vertex< fixed_tag > ft;

ft.toPoint();

Vertex< interval_tag > it;

it.toPoint();
}
HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
Joaquín Mª López Muñoz wrote in news:40******** *******@tid.es in
comp.lang.c++:

Seems to me like you're trying to specialize Vertex::toPoint ,
but C++ does not allow to specialize a single memfun. Several
alternatives come to mind:


In this case toPoint() doesn't need to be a template-member and
can be specialized (see my other reply in this thread).

Also the rule is that a template's template-member can't be
specialized unless the surrounding template is also specialized
(though this doesn't mean the body, see below and examples).
1. Specialize Vertex (the whole class) for both fixed_tag and
interval_tag. This forces you to duplicate all the common code.
Not good.


AFAICT there is no need to specialize the class itself inorder
to specialize a member, though I belive some compilers wrongly
require this.

Can do example
==============

#include <iostream>
#include <ostream>

struct X
{
template < typename T > void f();
};
template < typename T >
void X::f()
{
std::cout << "T\n";
}

template <>
void X::f< int >()
{
std::cout << "int\n";
}
int main()
{
using namespace std;

X x;

x.f<double>();
x.f<int>();
}
Template Example
=============== =

#include <iostream>
#include <ostream>

template < typename U >
struct X
{
template < typename T > void f();
};
template < typename U >
template < typename T >
void X< U >::f()
{
std::cout << "T\n";
}

template <>
template <>
void X< int >::f< int >()
{
std::cout << "int\n";
}
int main()
{
using namespace std;

{
X< double > x;

x.f<double>();
x.f<int>();
}
{
X< int > x;

x.f<double>();
x.f<int>();
}
}

Can't do Example
=============== =

Change the 2nd specialization above too:

template < typename U >
template <>
void X< U >::f< int >()
{
std::cout << "int\n";
}

There is probably some really good reason this can't be done
(other than "the standard says so" which is always good), but
I don't know what it is ?

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4


Leon ha escrito:
I'm having a bit of a template-problem, and I was wondering if you
guru's know what I'm doing wrong here...

I want to create a Vertex class that supports a toPoint() method. Now,
in my case there are 2 ways of calculating toPoint(), and I want to be
able to choose (at compile time) this method in a flexible way.
Therefore, I want to template this Vertex class.
For example, when I create a Vertex<fixed_ta g> I want the 'fixed'
toPoint()-method to be used, whereas when I create a
Vertex<interval _tag>, it should use the 'interval' variant.

What I've done so far is this:


Disregard my previous post, you *can* specialize the toPoint
memfun:

struct Point{};

struct fixed_tag{};
struct interval_tag{};

template<typena me S>
class Vertex{
public:
//
//Member-vars, constructor e.d.
//

Point toPoint() const;
};

//Implementation

template<>
Point Vertex<fixed_ta g>::toPoint() const{
//Calculate and return fixed point
return Point();
}

template<>
Point Vertex<interval _tag>::toPoint( ) const{
//Calculate and return fixed point
return Point();
}

int main()
{
Vertex<fixed_ta g> v;
v.toPoint();

Vertex<interval _tag> v2;
v2.toPoint();
}

Sorry about my previous response.

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo

Jul 22 '05 #5


Rob Williscroft ha escrito:
Joaquín Mª López Muñoz wrote in news:40******** *******@tid.es in
comp.lang.c++:

Seems to me like you're trying to specialize Vertex::toPoint ,
but C++ does not allow to specialize a single memfun. Several
alternatives come to mind:
In this case toPoint() doesn't need to be a template-member and
can be specialized (see my other reply in this thread).


Yes, you're right. I posted a revised answer shortly after I
realized my error.

[...]
Can't do Example
=============== =

Change the 2nd specialization above too:

template < typename U >
template <>
void X< U >::f< int >()
{
std::cout << "int\n";
}

There is probably some really good reason this can't be done
(other than "the standard says so" which is always good), but
I don't know what it is ?


I remember reading in Vandevoorde & Josutti's book that the
commitee simply dismissed this possibility as they thought it was
too difficult to implement. Curiously enough, MSVC++ 6.0
allows it (though only in inlined form, if I'm not wrong.)

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo

Jul 22 '05 #6
Joaquín Mª López Muñoz <jo*****@tid.es > wrote in message news:<40******* ********@tid.es >...
[snip]
Seems to me like you're trying to specialize Vertex::toPoint ,
but C++ does not allow to specialize a single memfun. Several
alternatives come to mind:

1. Specialize Vertex (the whole class) for both fixed_tag and
interval_tag. This forces you to duplicate all the common code.
Not good.
2. Factor the common code into a base class (say VertexBase) and
then proceed as in 1.
3. If you don't envision future variants of toPoint, the following is
a common idiom to do a compile-time selection of the implementation
of a memfun:

[snip]

I can think of a variation. The code that is to go with
the toPoint call could go in another class. Then the
template could have two template params, one the class
that is to be operated on, the other the class that holds
the toVertex implementation. Then instead of

template<class S>
class Vertex{
etc.

you would have

template<class S,class T>
class Vertex{
etc.

where you would pick T from some (small) set of classes each
of which held a particular toPoint implementation. Several
ways you could go from there. Questions like, when to use
friends, do you want static functions in the T's, etc.
Socks
Jul 22 '05 #7
Joaquín Mª López Muñoz <jo*****@tid.es > wrote in message news:<40******* ********@tid.es >...
Disregard my previous post, you *can* specialize the toPoint
memfun:

struct Point{};

struct fixed_tag{};
struct interval_tag{};

template<typena me S>
class Vertex{
public:
//
//Member-vars, constructor e.d.
//

Point toPoint() const;
};

//Implementation

template<>
Point Vertex<fixed_ta g>::toPoint() const{
//Calculate and return fixed point
return Point();
}

template<>
Point Vertex<interval _tag>::toPoint( ) const{
//Calculate and return fixed point
return Point();
}

int main()
{
Vertex<fixed_ta g> v;
v.toPoint();

Vertex<interval _tag> v2;
v2.toPoint();
}

Sorry about my previous response.

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Exactly, that was what I was trying to do! Thanks go to you and Rob
Williscroft, who also posted the same solution :)
Jul 22 '05 #8

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

Similar topics

1
1421
by: Howard | last post by:
Hi, I am using a very simple xslt file to get info from an xml document. The problem seems to me to be that the xml doc uses a namespace, and I don't know how to set up my xslt to recognize it correctly. If I remove the namespace specification from the xml, I can query the node I want perfectly. But with that namespace in there, it fails. Can someone help? Here's a simpilified version of the xml document, with that namespace stuff:
8
5108
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
0
1886
by: 42 | last post by:
I implemented a simple class inherited from Page to create a page template. It simply wraps some trivial html around the inherited page, and puts the inherited page into a form. The problem I have run into is that the emitted html at the end of the process is slightly different and doesn't work. Please don't be put off by all the source code. All the guts are in this first base class, and it doesn't do much. The rest is trivial...
2
1910
by: David Williams | last post by:
Hi all, I get errors when compiling the .cpp file below, both on Visual Studio and G++. 01: //------------------------------------------------- 02: template <typename Type> 03: class Outer 04: { 05: public: 06: Outer outerFunc(void); 07:
1
1361
by: mathieu | last post by:
Hello, I am trying to express a very simple piece of code. I have: #include <iostream> enum A { a1, a2, a3, a4 }; enum B { b1, b2, b3, b4 }; template <int TA, int TBstruct Foo {
7
2025
by: aaragon | last post by:
Hi everyone, The idea is quite simple: generate a container with random values in it. For that, I decided to create a class that I called RandomContainer that inherits from a container (with default value std::vector<T>). To handle the different ways that the values are randomized, I created a traits class. The problem that I have, is that the function randomize() within the traits class takes different parameters depending on the type...
2
1378
by: wagee | last post by:
I am trying to write a simple VBA program for a spreadsheet. However I am stuck. I am learning this as I go along so that is the big problem no doubt. I need to copy a sheet and rename. Simple eh? I copy it no problem but cannot figure how to change the copyed name (now firstname(2) for instance) to a new name. A cut out of the utility is below: Private Sub copysht() sname = "dog" Sheets("Template").Copy...
3
1849
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey Gurtovoy. I’m not doing this because I want to be a Meta Programming guru (because a lot of that stuff looks too crazy for use in the real world). Rather I want to learn heavyweight templates and this is the only
17
5812
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern "C" void* thread_entry(void*);
0
8689
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
9178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8916
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
5875
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
4376
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
4631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3058
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
3
2010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.