473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Template accepting partial type names?

Hi all,

I have some questions for template definition:

In the code bellow I could write:
#1: GenEditor<Mul(&vint, 5);
which compiles fine. But for my understanding Mul is not a typename,
because MUL itself is a template
which needs to have more information to becomes a full type. I expect:
#2: GenEditor<Mul<Var<int>,int(&vint, 5);
to be correct, but #1 works!

My first question: Is the code #1 correct and is it allways allowed to
give only parts of a template definition as
template parameter? Is this code accepted by most compilers. I
actually run under gcc 4.3.0.

The next question is to simplify my code by generate Objects of class
Editor direct, not indirect by using my GenEditor function template!
As I know, it is allowed to have template constructors in non template
classes, ok.
I can write a constructor for Editor which accepts the arguments like
Editor(Var<T>*, T) but
I am not able to give the needed template parameter Mul or Incr to it.
I can make the complete class Editor
to be a template and than I am able to give the parameter, but this
doubles the generated code for the
complete class, which is not what I would do.
What I want is something like :
Editor ed1<Mul>(&vint, 5);

Any suggestions?

Thanks a lot!
Regards
Klaus

Example Code:

#include <iostream>
using namespace std;

class VarBase {
public:
};

template <class T>
class Var: public VarBase {
private:
T content;

public:
Var(T _i): content(_i) {}
T Get() { return content; }

template <class X, class Yfriend class Incr;
template <class X, class Yfriend class Mul;
};

class OpBase {
public:
virtual void Up()=0;
virtual void Down()=0;
};

template <class VT, class T>
class Incr: public OpBase {
private:
T i;
VT *v;

public:
Incr(VT *vb, T _i): i(_i), v(vb) {}

void Up() { v->content+=i; }
void Down() { v->content-=i; }
};

template <class VT, class T>
class Mul: public OpBase {
private:
T i;
VT *v;

public:
Mul(VT *vb, T _i): i(_i), v(vb) {}

void Up() { v->content*=i; }
void Down() { v->content/=i; }
};

class EditorBase {
public:
virtual void Up()=0;
virtual void Down()=0;
};

class Editor : public EditorBase {
private:
OpBase *i;

public:
//template <template <class, classclass OP, class T, class
I>
Editor( OpBase* _i): i(_i){ }

void Up() { i->Up(); }
void Down() { i->Down(); }
};
template <template <class, classclass OP, class T>
EditorBase* GenEditor(Var<T>* var, T val) { return new Editor(new
OP<Var<T>, T>(var, val)); }

int main() {
Var<intvint(10);

EditorBase* e=GenEditor<Mul(&vint, 5);
//Editor e<Mul>(&vint, 5);

cout << "Wert ist:" << vint.Get() << endl;

e->Up();
cout << "Wert ist:" << vint.Get() << endl;

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

cout << "Wert ist:" << vint.Get() << endl;
EditorBase* e2=GenEditor<Incr(&vint, 3);
e2->Up();
cout << "Wert ist:" << vint.Get() << endl;

//-------------
cout << "-----------" << endl;

return 0;
}

Jul 4 '08 #1
0 1071

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

Similar topics

17
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...
6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
13
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
13
by: Imre | last post by:
Please take a look at the following code: template <typename T, typename Enable = void> class A { public: enum { value = 0 }; }; template <typename T>
6
by: rincewind | last post by:
Hi, can anybody summarise all options for partial template specialization, for all kind of parameters (type, nontype, template)? I *think* I understand options for partial specialization on...
7
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...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
1
by: ctoo | last post by:
The following compiles and works with g++ 3.4.4 and Borland C++ Builder 6 update#4: #include <iostream> #include <vector> #include <utility> // declaration and definition for primary class...
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.