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

Nested template type access

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
{
typedef T param_type;
};

template <typename U> struct B
{
typedef U::param_type nested_param_type; // line 10
};

int main()
{
B< A<int> > b;
return 0;
}

My compiler (gcc) throws this out with:

test.cpp:10: error: ISO C++ forbids declaration of `param_type' with no
type
test.cpp:10: error: `::param_type' is not a valid declarator
test.cpp:10: error: parse error before `;' token

(for some reason, gcc seems to think I am trying to declare
`param_type'). On other hand, the following code compiles happily:

// example2.cpp

template <typename T> struct A
{
const static int n = 123;
};

template <typename U> struct B
{
const static int m = U::n;
};

int main()
{
B< A<int> > b;
return 0;
}

In both cases the declaration of B references an attribute of its
template parameter U (a type in example 1, a static data member in
example 2) which is known - for a particular instantiation of an object
of type B - at compile time. It's not clear to me why the former should
be acceptable and the latter not.

Any views?

--
Lionel B

Jul 22 '05 #1
3 3824

"Lionel B" <go****@lionelb.com> wrote in message
news:10**********************@c13g2000cwb.googlegr oups.com...
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
{
typedef T param_type;
};

template <typename U> struct B
{
typedef U::param_type nested_param_type; // line 10


typedef typename U::param_type nested_param_type; // line 10

gcc (like any compliant compiler) is assuming that param_type is a static
data member. It is your responsibility to tell it different.

john
Jul 22 '05 #2
John Harrison wrote:
"Lionel B" <go****@lionelb.com> wrote in message
news:10**********************@c13g2000cwb.googlegr oups.com...
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
{
typedef T param_type;
};

template <typename U> struct B
{
typedef U::param_type nested_param_type; // line 10


typedef typename U::param_type nested_param_type; // line 10

gcc (like any compliant compiler) is assuming that
param_type is a static data member. It is your
responsibility to tell it different.


Thanks, John.

--
Lionel B

Jul 22 '05 #3

"Lionel B" <go****@lionelb.com> wrote in message
news:10**********************@c13g2000cwb.googlegr oups.com...
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
{
typedef T param_type;
};

template <typename U> struct B
{
typedef U::param_type nested_param_type; // line 10
};


Should be

typedef typename U::param_type nested_param_type'

Rgds
d

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 22 '05 #4

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

Similar topics

8
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...
2
by: CoolPint | last post by:
As a self-exercise, I am trying to write a generic Priority Queue, which would store any type and and accept any user-definable "priority" function. After much tinkering, I came up with...
5
by: Axter | last post by:
I'm fine tuning a scope_handle class that takes a policy class as the second template. http://code.axter.com/scope_handle.h Please see above link for full understanding of the problem. One...
1
by: Jacob N. Rohde | last post by:
Hi guys, I was under the assumption that the default type access modifier for classes/structs is internal. But the MCAD Study Guide (from MS even) says public?!?! (The Dr. GUI .NET tutorial from...
9
by: Mike | last post by:
Hi, Just a simple question: why the compiler doesn't report error when accessing a private member function inside a function having template type ? For example: #include<iostream> using...
1
by: Dusty1368 | last post by:
Good evening, I have this idea........ I want (someone) to design a template in Access or Excel (I was told it was these 2) and from which ever program data like date, time, location, and subject...
9
by: Adam Nielsen | last post by:
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...
3
by: Auslander | last post by:
I'm new to VBA, and trying to learn how to write some code to open a word template from access and populate it using Document Properties. Here's my code: Private Sub OpenWindows_Click() Dim...
8
by: Ralf Goertz | last post by:
Hi, I want to do able to declare an object bar of a templated class foo where the actual template type of foo is only know at runtime: ----- #include <string> template <class Tclass foo {
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: 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
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
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
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
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
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,...

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.