473,394 Members | 1,759 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,394 software developers and data experts.

Is it possible to use type traits as a template parameter?

I'm trying to create a base class who will contain generic algoritms
and derived class(es) who will perform problem-specific things. I'm
unable to use dynamic polymorphism, because base class know nothing
about problem-specific types. I wrote the following code using static
polymorphism, but compiler do not allow me to use type traits as a
template parameter, the following code not compiles:

template <class CT>
struct InfoClass
{
int id;
CT content;
};

template <class T, class TTraits>
struct SomeImpl
{
void go()
{
T *pT = static_cast<T*>(this);
pT->go_implementation();
}

void set_pointer(TTraits::ptrtype newptr)
{
ptr = newptr;
}

typedef InfoClass<TTraits::basetype> obj_type;
std::vector<obj_type> v;

TTraits::ptrtype ptr;
};

struct SomeClassTraits
{
typedef int basetype;
typedef char* ptrtype;
};

struct SomeClass : public SomeImpl<SomeClass, SomeClassTraits>
{
void go_implementation()
{
cout << "SomeClass.go()" << endl;

// use ptr as a char*
// use v as a vector<InfoClass<int> >
}
};

void TestTemplateImpl()
{
SomeClass x;
x.go();
}
Of course I can expand this
template <class T, class TTraits> struct SomeImpl
to the
template <class T, typename basetype, typename ptrtype, ...> struct
SomeImpl
but it will be a problem to maintain large code, for example:

// SomeImpl.h
template <class T, class TTraits>
struct SomeImpl
{
void go();
void set_pointer(TTraits::ptrtype newptr);
// a lot of member functions...
};
#include "SomeImpl.inl"

// SomeImpl.inl
template <class T, class TTraits>
inline void SomeImpl::go()
{
// implementation of go()
}

template <class T, class TTraits>
inline void SomeImpl::set_pointer(TTraits::ptrtype newptr);
{
// implementation of go()
}

If list of types changes, I need to rewrite "template<>" header for
each member function :-(

Are there any possibilities to pass type traits to template as a single
argument?

May 12 '06 #1
3 1586
Raider wrote:
I'm trying to create a base class who will contain generic algoritms
and derived class(es) who will perform problem-specific things. I'm
unable to use dynamic polymorphism, because base class know nothing
about problem-specific types. I wrote the following code using static
polymorphism, but compiler do not allow me to use type traits as a
template parameter, the following code not compiles:
Why doesn't it compile? What error messages did you get and where?

template <class CT>
struct InfoClass
{
int id;
CT content;
};

template <class T, class TTraits>
struct SomeImpl
{
void go()
{
T *pT = static_cast<T*>(this);
pT->go_implementation();
}

void set_pointer(TTraits::ptrtype newptr)

[snip rest of code]

You need "typename" before that type and all others within TTraits.

Cheers! --M

May 12 '06 #2
Raider wrote:
I'm trying to create a base class who will contain generic algoritms
and derived class(es) who will perform problem-specific things. I'm
unable to use dynamic polymorphism, because base class know nothing
about problem-specific types. I wrote the following code using static
polymorphism, but compiler do not allow me to use type traits as a
template parameter, the following code not compiles:
Of course it doesn't. It's missing some vital portions:

#include <vector>
#include <iostream>

template <class CT>
struct InfoClass
{
int id;
CT content;
};

template <class T, class TTraits>
struct SomeImpl
{
void go()
{
T *pT = static_cast<T*>(this);
pT->go_implementation();
}

void set_pointer(TTraits::ptrtype newptr)
void set_pointer(typename TTraits::ptrtype newptr)
{
ptr = newptr;
}

typedef InfoClass<TTraits::basetype> obj_type;
typedef InfoClass<typename TTraits::basetype> obj_type;
std::vector<obj_type> v;

TTraits::ptrtype ptr;
typename TTraits::ptrtype ptr;
};

struct SomeClassTraits
{
typedef int basetype;
typedef char* ptrtype;
};

struct SomeClass : public SomeImpl<SomeClass, SomeClassTraits>
{
void go_implementation()
{
cout << "SomeClass.go()" << endl;
std::cout << "SomeClass.go()" << std::endl;

// use ptr as a char*
// use v as a vector<InfoClass<int> >
}
};

void TestTemplateImpl()
{
SomeClass x;
x.go();
}
[...]

Are there any possibilities to pass type traits to template as a
single argument?


I think you need to learn about "dependent names".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 12 '06 #3
> You need "typename" before that type and all others within TTraits.

Oh, really!!! I forgot about "typename" keyword...
Thank you for quick answer.

May 12 '06 #4

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

Similar topics

2
by: Klaus Nowikow | last post by:
I would like to be able to do the following: std::cout // Or any other ostream << "Line 1\n" << push_tab << "Line 2\n" << "Line 3\n" << push_tab << "Line 4\n" << pop_tab
4
by: wkaras | last post by:
I would like to propose the following changes to the C++ Standard, the goal of which are to provide an improved ability to specify the constraints on type parameters to templates. Let me say from...
16
by: frs | last post by:
See example below: Why does the output of 'a' work and the output of 'b' fails to compile? Is there a way to write class 'something' so that 'b' converts correctly by default? (include iostream,...
2
by: maynard | last post by:
Is there a way to check if an object is an instance of a user-defined type or an integral type? I have a templated class that this would be very handy in (do _something_ if the template parameter...
5
by: Diego Martins | last post by:
Hi all. I want to build a template class to extract the return type of a pointer to function Something like: template<typename CB> class ReturnTypeExtractor { typedef ReturnType .... ...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
9
by: Bit Byte | last post by:
Can't seem to get my head around the point of a trait class - no matter how many times I read up on it - why not simply use functors or function pointers ? Anyone care to explain this in a...
5
by: Hong Ye | last post by:
Traits is a useful template technique to simplfy the implementation of some classes. however, I met some questions when I tried to understand and implement it. Following is an example of traits...
5
by: greek_bill | last post by:
Hi, I'm trying to develop a system where I can register some data/ information about a class. For example // ClassInfo.h template <class T> struct ClassInfo { static const std::string ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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,...
0
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...
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...

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.