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_tag> 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<interval_tag>
Point toPoint() const;
};
//Implementation
template<fixed_tag>
Point Vertex::toPoint() const{
//Calculate and return fixed point
}
template<interval_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. 7 1578
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_tag> 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>::toPoint(const fixed_tag&) const{
//Calculate and return fixed point
return Point();
}
template<class S>
Point Vertex<S>::toPoint(const interval_tag&) const{
//Calculate and return interval point
return Point();
}
int main()
{
Vertex<fixed_tag> 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
Leon wrote in news:8d**************************@posting.google.c om 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_tag> 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/
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/
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_tag> 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<typename S>
class Vertex{
public:
//
//Member-vars, constructor e.d.
//
Point toPoint() const;
};
//Implementation
template<>
Point Vertex<fixed_tag>::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_tag> 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
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
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
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<typename S> class Vertex{ public: // //Member-vars, constructor e.d. //
Point toPoint() const; };
//Implementation
template<> Point Vertex<fixed_tag>::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_tag> 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 :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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:...
|
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 {
|
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...
|
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?...
|
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...
|
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:
_________________________________________________________________
/*...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
| |