473,581 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to you access a nested template class?

Hi all,

I'm a bit confused about the syntax used to access a nested template
class. Essentially I have a bunch of class types to represent different
types of records in a database, and I want to store some cache data for
each datatype. The simplified code below demonstrates my issue - I
think I understand why it doesn't work as is (the structure only
declares the storage, I need to instantiate it for each template type)
but I'm not sure how to actually go about doing that.

Any pointers would be appreciated!

Thanks,
Adam.
class RecordTypeA { };
class RecordTypeB { };

class Main {

private:

template <typename T>
struct CacheData {
int i;
};

public:

template <typename T>
void set(T t, int x)
{
// Store some type-specific data for record type T
this->CacheData<T>:: i = x;
}

};

int main(void)
{
RecordTypeA A;
RecordTypeB B;
Main m;
m.set(A, 1);
m.set(B, 2);

// At this point, I want this to be the case:
// m.CacheData<A>: :i == 1
// m.CacheData<B>: :i == 2

return 0;
}
Nov 19 '07 #1
9 1957
Adam Nielsen wrote:
Hi all,

I'm a bit confused about the syntax used to access a nested template
class. Essentially I have a bunch of class types to represent different
types of records in a database, and I want to store some cache data for
each datatype. The simplified code below demonstrates my issue - I
think I understand why it doesn't work as is (the structure only
declares the storage, I need to instantiate it for each template type)
but I'm not sure how to actually go about doing that.

Any pointers would be appreciated!

Thanks,
Adam.
class RecordTypeA { };
class RecordTypeB { };

class Main {

private:

template <typename T>
struct CacheData {
int i;
};
This declares a type.
>
public:

template <typename T>
void set(T t, int x)
{
// Store some type-specific data for record type T
this->CacheData<T>:: i = x;
There is no member variable this->CacheData<Tsin ce CacheData<Tis a type.
}

};

int main(void)
{
RecordTypeA A;
RecordTypeB B;
Main m;
m.set(A, 1);
You cannot call set that way. Templates do not make types into valid
arguments for functions.
m.set(B, 2);

// At this point, I want this to be the case:
// m.CacheData<A>: :i == 1
// m.CacheData<B>: :i == 2

return 0;
}

You may want to ponder about the following:
#include <cassert>
#include <map>

template < typename T >
class typemap {

typedef void (*id) ( void );

template < typename A >
static
void type_identifier ( void ) {}

std::map< id, T data;

public:

template < typename A >
T const & value ( void ) const {
return ( data[ &type_identifie r<A] );
}

template < typename A >
T & value ( void ) {
return ( data[ &type_identifie r<A] );
}

};

int main ( void ) {
typemap<inttabl e;
table.value<cha r>() = 2;
table.value<int >() = 1;
assert( table.value<cha r>() == 2 );
assert( table.value<int >() == 1 );
}

Best

Kai-Uwe Bux

Nov 19 '07 #2
You may want to ponder about the following:
>

#include <cassert>
#include <map>

template < typename T >
class typemap {

typedef void (*id) ( void );

template < typename A >
static
void type_identifier ( void ) {}

std::map< id, T data;

public:

template < typename A >
T const & value ( void ) const {
return ( data[ &type_identifie r<A] );
}

template < typename A >
T & value ( void ) {
return ( data[ &type_identifie r<A] );
}

};

int main ( void ) {
typemap<inttabl e;
table.value<cha r>() = 2;
table.value<int >() = 1;
assert( table.value<cha r>() == 2 );
assert( table.value<int >() == 1 );
}
That makes sense, thanks for the example! Out of curiosity, is there
any way of doing this without using a runtime lookup? Something like a
'template-variable', if there were such a thing.

Cheers,
Adam.
Nov 20 '07 #3
Adam Nielsen wrote:
>You may want to ponder about the following:
#include <cassert>
#include <map>

template < typename T >
class typemap {

typedef void (*id) ( void );

template < typename A >
static
void type_identifier ( void ) {}

std::map< id, T data;

public:

template < typename A >
T const & value ( void ) const {
return ( data[ &type_identifie r<A] );
}

template < typename A >
T & value ( void ) {
return ( data[ &type_identifie r<A] );
}

};

int main ( void ) {
typemap<inttabl e;
table.value<cha r>() = 2;
table.value<int >() = 1;
assert( table.value<cha r>() == 2 );
assert( table.value<int >() == 1 );
}

That makes sense, thanks for the example! Out of curiosity, is there
any way of doing this without using a runtime lookup? Something like a
'template-variable', if there were such a thing.
You can do this for static data (e.g., you can have such data on a per class
basis):

#include <cassert>

template < typename T >
struct static_typemap {

template < typename A >
static
T & value ( void ) {
static T data;
return ( data );
}

};
int main ( void ) {

static_typemap< int>::value<cha r>() = 1;
static_typemap< int>::value<int >() = 2;

assert( static_typemap< int>::value<cha r>() == 1 );
assert( static_typemap< int>::value<int >() == 2 );

}
_If_ we had templated virtual member functions, we could do such tricks on a
per object basis (and all sorts of other cool stuff).
Best

Kai-Uwe Bux
Nov 20 '07 #4
You may want to ponder about the following:
>

#include <cassert>
#include <map>

template < typename T >
class typemap {

typedef void (*id) ( void );

template < typename A >
static
void type_identifier ( void ) {}

std::map< id, T data;

public:

template < typename A >
T const & value ( void ) const {
return ( data[ &type_identifie r<A] );
}

template < typename A >
T & value ( void ) {
return ( data[ &type_identifie r<A] );
}

};

int main ( void ) {
typemap<inttabl e;
table.value<cha r>() = 2;
table.value<int >() = 1;
assert( table.value<cha r>() == 2 );
assert( table.value<int >() == 1 );
}
Hi Kai-Uwe,

I've just started implementing this design in my code, but I can't
figure out why it won't work inside a template.

For example, if I delete your main() function above and replace the code
with this:

template <typename X>
int m ( void ) {
typemap<inttabl e;
table.value<cha r>() = 2; // line 59 in the error below
table.value<int >() = 1;
assert( table.value<cha r>() == 2 );
assert( table.value<int >() == 1 );
}

int main ( void ) {
m<int>();
}

When I compile it I end up with this:

t4.cpp: In function `int m()':
t4.cpp:59: error: parse error before `>' token
t4.cpp:60: error: parse error before `>' token
t4.cpp:61: error: parse error before `>' token
t4.cpp:62: error: parse error before `>' token

I don't understand why this happens, as I didn't think it mattered
*where* you used the code. Is there something special you must do in
this situation?

Thanks again,
Adam.
Nov 28 '07 #5
Adam Nielsen wrote:
>You may want to ponder about the following:
#include <cassert>
#include <map>

template < typename T >
class typemap {

typedef void (*id) ( void );

template < typename A >
static
void type_identifier ( void ) {}

std::map< id, T data;

public:

template < typename A >
T const & value ( void ) const {
return ( data[ &type_identifie r<A] );
}

template < typename A >
T & value ( void ) {
return ( data[ &type_identifie r<A] );
}

};

int main ( void ) {
typemap<inttabl e;
table.value<cha r>() = 2;
table.value<int >() = 1;
assert( table.value<cha r>() == 2 );
assert( table.value<int >() == 1 );
}

Hi Kai-Uwe,

I've just started implementing this design in my code, but I can't
figure out why it won't work inside a template.

For example, if I delete your main() function above and replace the code
with this:

template <typename X>
int m ( void ) {
typemap<inttabl e;
table.value<cha r>() = 2; // line 59 in the error below
table.value<int >() = 1;
assert( table.value<cha r>() == 2 );
assert( table.value<int >() == 1 );
}

int main ( void ) {
m<int>();
}

When I compile it I end up with this:

t4.cpp: In function `int m()':
t4.cpp:59: error: parse error before `>' token
t4.cpp:60: error: parse error before `>' token
t4.cpp:61: error: parse error before `>' token
t4.cpp:62: error: parse error before `>' token

I don't understand why this happens, as I didn't think it mattered
*where* you used the code. Is there something special you must do in
this situation?
I cannot reproduce the error. The code you posted compiles for me. This
maybe due to you giving instructions on how to create the code from the
present pieces instead of posting a complete piece.
Best

Kai-Uwe Bux
Nov 28 '07 #6
I cannot reproduce the error. The code you posted compiles for me. This
maybe due to you giving instructions on how to create the code from the
present pieces instead of posting a complete piece.
That's strange then - well here is the exact code I'm compiling.
Perhaps if someone else using GCC can test it as well to see whether
it's a compiler issue?

#include <cassert>
#include <map>

template < typename T >
class typemap {

typedef void (*id) ( void );

template < typename A >
static
void type_identifier ( void ) {}

std::map< id, T data;

public:

template < typename A >
T const & value ( void ) const {
return ( data[ &type_identifie r<A] );
}

template < typename A >
T & value ( void ) {
return ( data[ &type_identifie r<A] );
}

};

template <typename X>
int m ( void ) {
typemap<inttabl e;
table.value<cha r>() = 2;
table.value<int >() = 1;
assert( table.value<cha r>() == 2 );
assert( table.value<int >() == 1 );
}

int main ( void ) {
m<int>();
}

$ g++ -o t t.cpp
t.cpp: In function `int m()':
t.cpp:32: syntax error before `>' token
t.cpp:33: syntax error before `>' token
t.cpp:34: syntax error before `>' token
t.cpp:35: syntax error before `>' token
$ g++ --version
g++-gcc-3.2.3 (GCC) 3.2.3

I've also tried it with GCC 3.3.4 with the same result.

Cheers,
Adam.
Nov 28 '07 #7
Adam Nielsen wrote:
>I cannot reproduce the error. The code you posted compiles for me. This
maybe due to you giving instructions on how to create the code from the
present pieces instead of posting a complete piece.

That's strange then - well here is the exact code I'm compiling.
Perhaps if someone else using GCC can test it as well to see whether
it's a compiler issue?
[code snipped]
I've also tried it with GCC 3.3.4 with the same result.
Thanks for the complete code. It compiles for me with g++ (gcc 3.4.6 and
4.1.1).
Best

Kai-Uwe Bux
Nov 28 '07 #8
On Nov 28, 12:56 pm, Adam Nielsen
<adam.niel...@r emove.this.uq.e du.auwrote:
I cannot reproduce the error. The code you posted compiles
for me. This maybe due to you giving instructions on how to
create the code from the present pieces instead of posting a
complete piece.
That's strange then - well here is the exact code I'm
compiling. Perhaps if someone else using GCC can test it as
well to see whether it's a compiler issue?
#include <cassert>
#include <map>
template < typename T >
class typemap {
typedef void (*id) ( void );
template < typename A >
static
void type_identifier ( void ) {}
std::map< id, T data;
public:
template < typename A >
T const & value ( void ) const {
return ( data[ &type_identifie r<A] );
}
template < typename A >
T & value ( void ) {
return ( data[ &type_identifie r<A] );
}
};
template <typename X>
int m ( void ) {
typemap<inttabl e;
table.value<cha r>() = 2;
table.value<int >() = 1;
assert( table.value<cha r>() == 2 );
assert( table.value<int >() == 1 );
}
int main ( void ) {
m<int>();
}
$ g++ -o t t.cpp
t.cpp: In function `int m()':
t.cpp:32: syntax error before `>' token
t.cpp:33: syntax error before `>' token
t.cpp:34: syntax error before `>' token
t.cpp:35: syntax error before `>' token
$ g++ --version
g++-gcc-3.2.3 (GCC) 3.2.3
I've also tried it with GCC 3.3.4 with the same result.
I get the same errors with 3.2.3, but it compiles with g++
4.1.0. If you replace typemap<intwith typemap<Xin the first
line of m, you get errors with both compilers, of course.
Changing each of the function calls to:

table.template value<...>...

works in all cases however.

It's an interesting case, since it's the only case I know of off
hand in which a (non-instantiated) template definition can
trigger the instantiation of another template. Apparently, g++
pre-4.0 is not instantiating typemap<inthere , and so is
treating the < as if it were the less than operator. (Note that
in the case of typemap<X>, the context is dependent, so you are
required to tell the compiler when the name in question -- here,
value -- is a type or a template.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 29 '07 #9
Changing each of the function calls to:
>
table.template value<...>...

works in all cases however.
Ah, that's excellent - that's saved me a compiler upgrade :-)

Cheers,
Adam.
Nov 30 '07 #10

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

Similar topics

3
2060
by: Andriy Shnyr | last post by:
Let us consider the following nested templates case: template<typename T> class Outer{ public: template<typename U> class Inner{ public:
8
3938
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access private members of nesting class. template <typename T> class Container { // NO friendship given to any other public: class ContainerIterator;
1
2072
by: Chris Schadl | last post by:
Okay, I'm having a bit of a brain-fart and I can't remember how I would do this. Say I have the following: template <typename T1, typename T2> class A; // Forward declaration of A template <typename T1, typename T2> class B {
3
3844
by: Lionel B | last post by:
Greetings. In a template class for which a template parameter may be another template class, I would like to be have access to the type of the nested template parameter. Hopefully the following example makes this clearer: // example1.cpp template <typename T> struct A
8
16879
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. This works fine if all of the properties are at the top (root level) of the model but I'd like to keep them in nested classes to organize them...
4
2073
by: Mr Dyl | last post by:
I'm trying to declare the following friendship and VS.Net 2003 is complaining: template <class T> class Outter { class Inner {...} ... }
3
3877
by: jdurancomas | last post by:
Dear all, I'm trying to declare the operator++ to a nested class. The nested class is not template but the container it is. The code used in teh sample program is included bellow: #include <iostream>
5
3730
by: huili80 | last post by:
For example, like in the following, the part commented out was intended as partial spectialzation, but it would even compile. Is it even legal to partially specialize a nested template class inside another template class? template < typename T > struct A { template < typename U > struct B
2
2280
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include <stack> template <class T> class A { public:
0
7804
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7910
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...
0
8180
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6563
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5681
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3809
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...
0
3832
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2307
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
1
1409
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.