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

Declaring Template Classes that take Multiple Nested Templates as Parameters

Hello,

I'm having some difficulty compiling template classes as containers for
other template objects. Specifically, I have a hierarchy of template
classes that contain each other. Template class B has an instance of
template class A, which has some base type T (usually int or double).
However, the base type T is important to calculations in B, so I would
like to obtain access to the type for further variable declaration
within B. Below is a simplified version of my best attempt at the
syntax. I'm compiling under XCode 2.0 on Mac OS X version 10.4.6 using
GCC 4. Error messages have been commented in after lines in which they
occur:
//=========================================
#include <iostream>
#include <vector>

template <typename T> class A
{
typedef T base_type;

public:
base_type get_item(int index)
{
return data_(index);
}

private:
std::vector<base_type> data_;
};

template <template <typename T> class A> class B
{
typedef T base_type; // error: 'T' does not name a type
typedef A<base_type> Atype; // error: 'base_type' was not declared
in this scope

public:
base_type get_item(int index)
{
return data_.get_item(index);
}

private:
Atype data_;

};
//=========================================
Is there any way to gain access to these nested template parameters
from within class B? Is there a better way to get this same
functionality without intimate knowledge of A's types? Am I just
making some syntax error in the template class declaration?

Thanks for the help!

Jun 9 '06 #1
2 3315

pag...@gmail.com wrote:
Hello,

I'm having some difficulty compiling template classes as containers for
other template objects. Specifically, I have a hierarchy of template
classes that contain each other. Template class B has an instance of
template class A, which has some base type T (usually int or double).
However, the base type T is important to calculations in B, so I would
like to obtain access to the type for further variable declaration
within B. Below is a simplified version of my best attempt at the
syntax. I'm compiling under XCode 2.0 on Mac OS X version 10.4.6 using
GCC 4. Error messages have been commented in after lines in which they
occur:
//=========================================
#include <iostream>
#include <vector>

template <typename T> class A
{
typedef T base_type;

public:
base_type get_item(int index)
{
return data_(index);
}

private:
std::vector<base_type> data_;
};

template <template <typename T> class A> class B
{
typedef T base_type; // error: 'T' does not name a type
typedef A<base_type> Atype; // error: 'base_type' was not declared
in this scope

public:
base_type get_item(int index)
{
return data_.get_item(index);
}

private:
Atype data_;

};
//=========================================
Is there any way to gain access to these nested template parameters
from within class B? Is there a better way to get this same
functionality without intimate knowledge of A's types? Am I just
making some syntax error in the template class declaration?

Thanks for the help!


The template parameter T that you have defined in the template class B
is really a template parameter for the template template parameter A.

It isnt visible outside. In many ways it is just a holder when
declaring a template template parameter and infact if you dont refer to
it again, you dont even need to have a name for that.

anyway try this.
template <typename T> class A
{
typedef T base_type;

public:
base_type get_item(int index)
{
return data_(index);
}

private:
std::vector<base_type> data_;

};

template < typename T, template <typename> class A> class B
{
typedef T base_type; // error: 'T' does not name a type

typedef A<base_type> Atype; // error: 'base_type' was not
declared in this scope

public:
base_type get_item(int index)
{
return data_.get_item(index);
}
private:
Atype data_;
};

Jun 9 '06 #2
am******@gmail.com wrote:
pag...@gmail.com wrote:
Hello,

I'm having some difficulty compiling template classes as containers for
other template objects. Specifically, I have a hierarchy of template
classes that contain each other. Template class B has an instance of
template class A, which has some base type T (usually int or double).
However, the base type T is important to calculations in B, so I would
like to obtain access to the type for further variable declaration
within B. Below is a simplified version of my best attempt at the
syntax. I'm compiling under XCode 2.0 on Mac OS X version 10.4.6 using
GCC 4. Error messages have been commented in after lines in which they
occur:
//=========================================
#include <iostream>
#include <vector>

template <typename T> class A
{
typedef T base_type;

public:
base_type get_item(int index)
{
return data_(index);
}

private:
std::vector<base_type> data_;
};

template <template <typename T> class A> class B
{
typedef T base_type; // error: 'T' does not name a type
typedef A<base_type> Atype; // error: 'base_type' was not declared
in this scope

public:
base_type get_item(int index)
{
return data_.get_item(index);
}

private:
Atype data_;

};
//=========================================
Is there any way to gain access to these nested template parameters
from within class B? Is there a better way to get this same
functionality without intimate knowledge of A's types? Am I just
making some syntax error in the template class declaration?

Thanks for the help!


The template parameter T that you have defined in the template class B
is really a template parameter for the template template parameter A.

It isnt visible outside. In many ways it is just a holder when
declaring a template template parameter and infact if you dont refer to
it again, you dont even need to have a name for that.

anyway try this.
template <typename T> class A
{
typedef T base_type;

public:
base_type get_item(int index)
{
return data_(index);
}

private:
std::vector<base_type> data_;

};

template < typename T, template <typename> class A> class B
{
typedef T base_type; // error: 'T' does not name a type

typedef A<base_type> Atype; // error: 'base_type' was not
declared in this scope

public:
base_type get_item(int index)
{
return data_.get_item(index);
}
private:
Atype data_;
};


Or...

template <typename T> class A
{
public:
typedef T base_type; // now public

base_type get_item(int index)
{
return data_(index);
}
private:
std::vector<base_type> data_;
};

template <class A> class B
{
typedef typename A::base_type base_type;

public:
base_type get_item(int index)
{
return data_.get_item(index);
}
private:
A data_;
};

Cheers! --M

Jun 9 '06 #3

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

Similar topics

8
by: Thomas Heller | last post by:
I need to convert C preprocessor definitions into python code. The definitions are dumped out of gccxml (see http://www.gccxml.org) , running over the windows header files (for example). This...
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...
10
by: Clay_Culver | last post by:
I have heard that it is possible to use classes + template magic to make standalone functions (or maybe just a functor which acts like a function) which can accept variable length arguments without...
3
by: Steve | last post by:
Is there any way of specifying the startMode when using the xslTransform class? We are updating code which used msxml to the system.xml classes but can find no way to specify the startMode. We...
3
by: sks | last post by:
Hello all Is the usage of extern keyword valid for telling the compiler to NOT instantiate a template and to link it from an another binary? For example: Suppose module A's binary contains a...
4
by: Schüle Daniel | last post by:
Hello all, my question is basically wheather it's possible to have nested templates .. I mean the following if there are one templ. class and one global templ. function is it possible to...
2
by: coolpint | last post by:
Can anyone kindly provide an explanation as to why the compiler does not "see" the function template in the contrieved code below? I think the argument deduction fails but can't figure out...
4
by: festo | last post by:
Hi guys, I need to print a different footer depending on the template being printed. How do I test for the current template being processed in XSLT? Thanks, fes
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. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: 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
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,...

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.