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

Trying to use templated types as parameters in template specialization.

I am using the CRTP (Curiously Recurring Template Pattern) to provide a
set of low-level functions customizable by environment. Using CRTP
ensures that all the function have the same signatures, which becomes
important as the interfaces grow, especially where hardware interfaces
are involved.

For example:

// Feature set for the environment...
template< class Implementation >
class Interface
{
public: // Define a function and its signature...
static void do_it( int );
};
// Definition for real environment...
class Hardware
: public Interface< Hardware >
{
// Nothing here as everything is in Interface<Hardware>
// so signatures *must* match.
};

// Implementation of real environment...
template<>
void Interface< Hardware >::do_it( int i ) { /* Hack i */ }
// Definition for test harness...
class Simulated
: public Interface<Simulated>
{
public:
// Nothing here as everything is in Interface<Simulated>
// so signatures *must* match.

private:
static int it; // Implementation of test harness.
friend class Interface<Simulated>;
};

// Implementation of test harness...
template<>
void Interface<Simulated>::do_it( int i ) { Simulated::it = i; }

So far, this works fine, but I want to add another level of
templatization. Wheeler's Law applies precisely: "Any problem in
computer science can be solved with another layer of indirection. But
that usually will create another problem."

My intent was to create a logging version of each implementation, to
allow sessions to be recorded for analysis, etc...

template< class Implementation >
class Logged
: public Interface< Logged<Implementation
{
public:
// Nothing here as everything is in Interface<Simulated>
// so signatures *must* match.

private:
static void log( const char *s );
};

// Partially specialize to implement logging of arbitrary
implementations.
template< class Implementation >
void Interface< Logged<Implementation::do_it( int i )
{
Logged<Implementation>::log("doing it");
Implementation::do_it( i );
}

The final specialization doesn't compile: g++ gives:
error: invalid use of undefined type 'class
Interface<Logged<Implementation'

and Comeau gives the clearer (but no more useful):
error: template argument list must match the parameter list

Varying the template declaration syntax mutates the errors but hasn't
helped. There is a simple workaround, defining the functions in the
Logged template itself, but this returns to the original problem -
changes to function signatures are not checked.

Thanks,

David O.

Jan 5 '07 #1
2 2447
David O wrote:
I am using the CRTP (Curiously Recurring Template Pattern) to provide
a set of low-level functions customizable by environment. [..]

// Partially specialize to implement logging of arbitrary
implementations.
template< class Implementation >
void Interface< Logged<Implementation::do_it( int i )
{
Logged<Implementation>::log("doing it");
Implementation::do_it( i );
}
You cannot partially specialise a member, it's not a template.
You can, however, define a member for partially specialised class
template. But you have to partially specialise the class template
first.
The final specialization doesn't compile: g++ gives:
error: invalid use of undefined type 'class
Interface<Logged<Implementation'
Yes, you cannot define a member of an undefined type.
and Comeau gives the clearer (but no more useful):
error: template argument list must match the parameter list
Clearer? I am not so sure. Interface<Logged<Implementation has
not beed defined. You simply are not allowed to define a member of
an undefined class. Now, when you define a member of a [fully]
specialised template, it causes *instantiation* (and specialisation)
of that template. It's not the case with partial specialisation.
Varying the template declaration syntax mutates the errors but hasn't
helped. There is a simple workaround, defining the functions in the
Logged template itself, but this returns to the original problem -
changes to function signatures are not checked.
You will have to partially specialise the template first (and while
you're at it, define the actual member function).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 5 '07 #2
Victor Bazarov wrote:
<snip>
You cannot partially specialise a member, it's not a template.
You can, however, define a member for partially specialised class
template. But you have to partially specialise the class template
first.
Victor,

Thanks for the prompt reply [delayed replying myself due to a stinking
cold].

I was suspecting that what I was trying top achieve was impossible.
Other "obvious" variations, such as this (naturally) fail:

template< class Implementation >
class Interface< Logged<Implementation;

My real question is whether it is possible to write something that
works the way that a casual first reading of my example would seem to
do.
You will have to partially specialise the template first (and while
you're at it, define the actual member function).
This will achieve the functionality, but has the same problem as
declaring the functions in the Logged template, that of ensuring all
function signatures match - other than diligent eyeballing (or even
macros) - which is the reason the CRTP mechanism was chosen in the
first place.

In the simplified example, keeping track of function signatures is
trivial. In the real world situation, the class has an unavoidably wide
interface (it models a hardware/OS environment) and has several
varients and authors, so ensuring signature changes propagate correctly
is an important issue.

Thanks,

David O.

Jan 10 '07 #3

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

Similar topics

7
by: Saulius | last post by:
template<template<typename T> class StoragePolicy> class construction_info { // Empty by default, specialize for other types. }; template<template<typename> class StoragePolicy, typename...
2
by: Chris Foster | last post by:
Hi, I'm having some difficulty using types which are defined in a base class inside a derived class. The problem crops up using template classes. The following test code encapsulates what I'd...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
3
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename...
6
by: __PPS__ | last post by:
it's ok according to the standard to specialize member function like this: template<class T>struct xxx { void func(); }; template<>bool xxx<int>::func(){ //int version } template<>bool...
5
by: Gert Van den Eynde | last post by:
Hi all, It's probably trivial but I can't figure it out... I have a templated class template <typename T, typename uclass A I wish to fill a vector with pointers to objects of class A. I...
14
by: Dave Rahardja | last post by:
Hi all, Is it possible to prevent _at compile or link time_ the mulitple instantiation of a templated function? In other words, if there exists a function template <typename Tvoid fn(); I...
12
by: Robert.Holic | last post by:
Hi All (first time caller, long time listener), I've stumbled across a problem that I have yet to figure out, although Im sure I'll kick myself when I figure it out. Here it is: I need to...
2
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.