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

templates and local types

Hi,

Consider:

template<typename Tclass Z {};
int doSomething(void) {
class X {};
Z<Xz;
}

Any idea how this can be made compilable while keeping class X local?

The example has been simplified significantly. The reason why class X
has to be local is because doSomething() is a template function and
class X needs to be instantiated with the same type as doSomething().

Thanks for any help,

Christof
Aug 16 '06 #1
5 1124
"Christof Warlich" <cw******@gmx.dewrote in message
news:44**********************@newsspool3.arcor-online.net
Hi,

Consider:

template<typename Tclass Z {};
int doSomething(void) {
class X {};
Z<Xz;
}

Any idea how this can be made compilable while keeping class X local?

The example has been simplified significantly. The reason why class X
has to be local is because doSomething() is a template function and
class X needs to be instantiated with the same type as doSomething().
In that case you have simplified the problem to the point of removing
essential features. I take it that doSomething and X are both templates.
Then what is wrong with:

template<typename Tclass Z {};

template<typename Tclass X {};

template<typename T>
int doSomething(void)
{
Z< X<T z;
return 0;
}

--
John Carson
Aug 16 '06 #2
John Carson wrote:
In that case you have simplified the problem to the point of removing
essential features. I take it that doSomething and X are both templates.
Then what is wrong with:

template<typename Tclass Z {};

template<typename Tclass X {};

template<typename T>
int doSomething(void)
{
Z< X<T z;
return 0;
}
You are right, I've probably stripped down things too much. But some
additional explanations may do:

My intention was to separate template declaration and definition (See
FAQ Lite: How can I avoid linker errors with my template functions).
Doing this, I wanted to minimize the lines of code that have to be
written to explicitly instantiate my templates (in this simple case,
just instantiating doSomething() for the required types instead of
having to instantiate doSomething() and class X).

Aug 16 '06 #3
"Christof Warlich" <cw******@gmx.dewrote in message
news:44**********************@newsspool1.arcor-online.net
John Carson wrote:
>In that case you have simplified the problem to the point of removing
essential features. I take it that doSomething and X are both
templates. Then what is wrong with:

template<typename Tclass Z {};

template<typename Tclass X {};

template<typename T>
int doSomething(void)
{
Z< X<T z;
return 0;
}
You are right, I've probably stripped down things too much. But some
additional explanations may do:

My intention was to separate template declaration and definition (See
FAQ Lite: How can I avoid linker errors with my template functions).
Doing this, I wanted to minimize the lines of code that have to be
written to explicitly instantiate my templates (in this simple case,
just instantiating doSomething() for the required types instead of
having to instantiate doSomething() and class X).

You could turn your function into a functor, like std::cout. To get this to
work of course, you need to declare an object of the class, either (i) in
client code or (ii) in the implementation file, with an extern declaration
in the header. This gives an extra line (or two), so you may not think this
approach offers an advantage --- though it still could if you had classes A,
B, C... as well as X.

First, a version where it all goes in the header:

template<typename T>
class Z {};

template<typename T>
class doSomething
{
class X {};
public:
int operator()()
{
Z<Xz;
return 0;
}
};

// need to also declare an object of the class
Splitting this into .h and .cpp files, we would have:

////// foo.h file ///////////////////////////

template<typename T>
class doSomething
{
class X;
public:
int operator()();
};

extern doSomething<intds;
/////// foo.cpp file //////////////////////

#include "foo.h"

template<typename T>
class Z {};

template<typename T>
class doSomething<T>::X
{};

template<typename T>
int doSomething<T>::operator()()
{
Z<Xz;
return 0;
}

// only one instantiation line required
template class doSomething<int>;

// but need this line unless client code has it
doSomething<intds;

//////// main.cpp file /////////////////////

#include "foo.h"

int main()
{
ds();
}

/////////// end files ///////////////////////

The above seems to work with VC++ 2005.

--
John Carson
Aug 16 '06 #4
Christof Warlich wrote:
Hi,

Consider:

template<typename Tclass Z {};
int doSomething(void) {
class X {};
Z<Xz;
}

Any idea how this can be made compilable while keeping class X local?
Can't be done. You can move doSomething and X into a namespace,
but templates can't have local types.
Aug 16 '06 #5
John Carson schrieb:
You could turn your function into a functor, like std::cout. To get this to
work of course, you need to declare an object of the class, either (i) in
client code or (ii) in the implementation file, with an extern declaration
in the header. This gives an extra line (or two), so you may not think this
approach offers an advantage --- though it still could if you had classes A,
B, C... as well as X.
Hi John,

thanks a lot, that's an interesting approach. I'll go and play around
with it today. But anyhow, I realized that I reached my goal also with
your very first suggestion: It was sufficient to instantiate doSomething
even when class X was not local; class X seems then to be instantiated
as well.

Regards,

Christof
Aug 17 '06 #6

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

Similar topics

22
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
2
by: Niklas Norrthon | last post by:
I want to share a technique I recently have found to be useful to get around some obstacles that data protection can raise. Consider the following class: // foo.h #ifndef H_FOO #define H_FOO...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
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...

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.