473,398 Members | 2,120 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,398 software developers and data experts.

template related query

I've a templatized class member function calling templatized member
function of another class.

//A.h
template <class T>
class A {
public:
static T* create();
};

//B.h
class B {

template<class T>
T* makeData();

};

template<class T>
T* B::makeData()
{
return A<T>::create();
}

I get a compile error (gcc 3.3.6) when I include B.h in some X.cpp.
(Error is on the line where I include, not where I instantiate).

But I don't get compile error with gcc 4.2.2. Is there any way that I
can avoid this error in gcc3.3.6?

Regards,
~ Soumen
Oct 10 '08 #1
12 1372
On Oct 10, 6:22*pm, Soumen <soume...@gmail.comwrote:
I've a templatized class member function calling templatized member
function of another class.

//A.h
template <class T>
class A {
public:
static T* * * *create();

};

//B.h
class B {
public:
template<class T>
T* * * * makeData();

};

template<class T>
T* * * *B::makeData()
{
* * * *return A<T>::create();

}

I get a compile error (gcc 3.3.6) when I include B.h in some X.cpp.
(Error is on the line where I include, not where I instantiate).

But I don't get compile error with gcc 4.2.2. Is there any way that I
can avoid this error in gcc3.3.6?

Regards,
~ Soumen
Little correction A is not templatized class, rather create is
templatized method.

class A {
public:
template <class T>
static T* create();
};

Also, B::makeData() is like following:

//B.h
class B {
public:
template<class T>
T* makeData();

};

template<class T>
T* B::makeData()
{
return A::create<T>();
}
Oct 10 '08 #2
Soumen wrote:
[...]
I get a compile error (gcc 3.3.6) when I include B.h in some X.cpp.
(Error is on the line where I include, not where I instantiate).
So it must suspect you've done something wrong there.
But I don't get compile error with gcc 4.2.2. Is there any way that I
can avoid this error in gcc3.3.6?
Post the exact code that reproduces the error, tell us the
exact error message, and we can begin to speculate about the
compiler's reasoning and whether it's right or wrong.
Don't be as specific, and the answers probably won't get more
specific than mine from above.
~ Soumen
Schobi
Oct 10 '08 #3
Hendrik Schober wrote:
Soumen wrote:
>[...]
I get a compile error (gcc 3.3.6) when I include B.h in some X.cpp.
(Error is on the line where I include, not where I instantiate).

So it must suspect you've done something wrong there.
... or the compiler is incapable of processing the code because it is
malfunctioning due to a bug or to a misinterpretation of the Standard.
There is no way to tell for sure.
>
>But I don't get compile error with gcc 4.2.2. Is there any way that I
can avoid this error in gcc3.3.6?

Post the exact code that reproduces the error, tell us the
exact error message, and we can begin to speculate about the
compiler's reasoning and whether it's right or wrong.
Don't be as specific, and the answers probably won't get more
specific than mine from above.
Most likely the difference between versions is because the compiler
developers have caught up with the Standard or fixed some bugs in their
product. It would be wise to ask this question in the GCC forum [as
well as, not instead of, here].
>
>~ Soumen

Schobi
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 10 '08 #4
Victor Bazarov wrote:
Hendrik Schober wrote:
>Soumen wrote:
>>[...]
I get a compile error (gcc 3.3.6) when I include B.h in some X.cpp.
(Error is on the line where I include, not where I instantiate).
So it must suspect you've done something wrong there.

... or the compiler is incapable of processing the code because it is
malfunctioning due to a bug or to a misinterpretation of the Standard.
What would be the difference?
[...]
V
Schobi
Oct 10 '08 #5
Hendrik Schober wrote:
Victor Bazarov wrote:
>Hendrik Schober wrote:
>>Soumen wrote:
[...]
I get a compile error (gcc 3.3.6) when I include B.h in some X.cpp.
(Error is on the line where I include, not where I instantiate).
So it must suspect you've done something wrong there.

... or the compiler is incapable of processing the code because it is
malfunctioning due to a bug or to a misinterpretation of the Standard.

What would be the difference?
The difference, as I understand it, is _who_ had done something wrong: the
programmer of the code or the programmer of the compiler.
Best

Kai-Uwe Bux
Oct 10 '08 #6
Kai-Uwe Bux wrote:
Hendrik Schober wrote:
>Victor Bazarov wrote:
>>Hendrik Schober wrote:
Soumen wrote:
[...]
I get a compile error (gcc 3.3.6) when I include B.h in some X.cpp.
(Error is on the line where I include, not where I instantiate).
So it must suspect you've done something wrong there.
... or the compiler is incapable of processing the code because it is
malfunctioning due to a bug or to a misinterpretation of the Standard.
What would be the difference?

The difference, as I understand it, is _who_ had done something wrong: the
programmer of the code or the programmer of the compiler.
If I say the compiler /suspects/ the programmer has done
something wrong, then who's accused? :)
Kai-Uwe Bux
Schobi
Oct 14 '08 #7
Hendrik Schober wrote:
Kai-Uwe Bux wrote:
>Hendrik Schober wrote:
>>Victor Bazarov wrote:
Hendrik Schober wrote:
Soumen wrote:
>[...]
>I get a compile error (gcc 3.3.6) when I include B.h in some X.cpp.
>(Error is on the line where I include, not where I instantiate).
So it must suspect you've done something wrong there.
... or the compiler is incapable of processing the code because it is
malfunctioning due to a bug or to a misinterpretation of the Standard.
What would be the difference?

The difference, as I understand it, is _who_ had done something wrong:
the programmer of the code or the programmer of the compiler.

If I say the compiler /suspects/ the programmer has done
something wrong, then who's accused? :)
The programmer and not the compiler. In the vast majority of cases, the
compilers suspicions are right. I get compiler errors almost all the time,
but I have found only 10 compiler bugs so far.
Best

Kai-Uwe Bux
Oct 14 '08 #8
Kai-Uwe Bux wrote:
[...] I get compiler errors almost all the time,
but I have found only 10 compiler bugs so far.
You need to do more porting. Or more template stuff. Or both. :)
Kai-Uwe Bux
Schobi
Oct 15 '08 #9
On Oct 15, 1:46*pm, Hendrik Schober <spamt...@gmx.dewrote:
Kai-Uwe Bux wrote:
[...] I get compiler errors almost all the time,
but I have found only 10 compiler bugs so far.
* You need to do more porting. Or more template stuff. Or both. :)
It depends on what his boss is paying him to do: write working
(and maintainable) code, or stress test compilers. With modern
compilers, it's actually fairly rare to stumble on a compiler
bug unless you're pushing the envelope well beyond what is
reasonable in a normal production environment.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 15 '08 #10
James Kanze wrote:
On Oct 15, 1:46 pm, Hendrik Schober <spamt...@gmx.dewrote:
>Kai-Uwe Bux wrote:
>>[...] I get compiler errors almost all the time,
but I have found only 10 compiler bugs so far.
> You need to do more porting. Or more template stuff. Or both. :)

It depends on what his boss is paying him to do: write working
(and maintainable) code, or stress test compilers. With modern
compilers, it's actually fairly rare to stumble on a compiler
bug unless you're pushing the envelope well beyond what is
reasonable in a normal production environment.
In the last decade I have written code that needed to compile
using several versions of Borland, Microsoft, Metrowerks, GCC,
multiplied with several std lib implementations (and versions
thereof) and, at least with GCC, several platforms. Although,
in my experience, too, most of the time when at first I thought
I'd found a compiler error I have found one of my own, I did
find several dozen of compiler errors in that time -- and not
just in obscure corner cases. And quite a lot of my 10 years
old code is still in use (although it doesn't need all that
much maintenance nowadays).
But, yes, I was paid to write portable code (and to get compiler
vendors to send us hotfixes).

Schobi
Oct 18 '08 #11
On Oct 18, 9:48*pm, Hendrik Schober <spamt...@gmx.dewrote:
James Kanze wrote:
On Oct 15, 1:46 pm, Hendrik Schober <spamt...@gmx.dewrote:
Kai-Uwe Bux wrote:
[...] I get compiler errors almost all the time,
but I have found only 10 compiler bugs so far.
* You need to do more porting. Or more template stuff. Or
both. :)
It depends on what his boss is paying him to do: write
working (and maintainable) code, or stress test compilers.
*With modern compilers, it's actually fairly rare to stumble
on a compiler bug unless you're pushing the envelope well
beyond what is reasonable in a normal production
environment.
* In the last decade I have written code that needed to compile
* using several versions of Borland, Microsoft, Metrowerks, GCC,
* multiplied with several std lib implementations (and versions
* thereof) and, at least with GCC, several platforms.
There are two ways of doing that. One is writing very up to
date C++, with platform dependent work-arounds for errors or
unsupported features. The other is to simply write code using
the common subset of C++ which actually works on all target
compilers. Twenty-five or thirty years ago, the common subset
which worked was very, very small---if you throw in g++ 1.49
(the first compiler I used professionally), it didn't even
include local variables with a destructor. Today, it's a
reasonable subset for most uses, even if you throw in what are
today some very old compilers. And most applications should
take this approach. There are exceptions---some of what Boost
tries to do is intentially pushing the envelope, for example,
legitimately. But be aware that pushing the envelope can be
very expensive, and most commercial operations won't reap any
real benefit from the added cost.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 19 '08 #12
James Kanze wrote:
On Oct 18, 9:48 pm, Hendrik Schober <spamt...@gmx.dewrote:
>James Kanze wrote:
>>On Oct 15, 1:46 pm, Hendrik Schober <spamt...@gmx.dewrote:
Kai-Uwe Bux wrote:
[...] I get compiler errors almost all the time,
but I have found only 10 compiler bugs so far.
>>> You need to do more porting. Or more template stuff. Or
both. :)
>>It depends on what his boss is paying him to do: write
working (and maintainable) code, or stress test compilers.
With modern compilers, it's actually fairly rare to stumble
on a compiler bug unless you're pushing the envelope well
beyond what is reasonable in a normal production
environment.
> In the last decade I have written code that needed to compile
using several versions of Borland, Microsoft, Metrowerks, GCC,
multiplied with several std lib implementations (and versions
thereof) and, at least with GCC, several platforms.

There are two ways of doing that. One is writing very up to
date C++, with platform dependent work-arounds for errors or
unsupported features. The other is to simply write code using
the common subset of C++ which actually works on all target
compilers. Twenty-five or thirty years ago, the common subset
which worked was very, very small---if you throw in g++ 1.49
(the first compiler I used professionally), it didn't even
include local variables with a destructor. Today, it's a
reasonable subset for most uses, even if you throw in what are
today some very old compilers. And most applications should
take this approach. There are exceptions---some of what Boost
tries to do is intentially pushing the envelope, for example,
legitimately. But be aware that pushing the envelope can be
very expensive, and most commercial operations won't reap any
real benefit from the added cost.
We were trying to find some middle ground: Push the envelop, but
only so far. :)
Code according to the smallest common sub set had been written in
that company for half a decade. We changed that and tried to apply
more modern techniques. And we found it worked well. When I started
there, the common error report from the field was a crash report,
usually accompanied by data loss. Nowadays, it's that a dialog
popped up telling it found some unexpected error -- and those are
a lot rarer.

Schobi
Oct 20 '08 #13

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

Similar topics

1
by: Istvan Buki | last post by:
Dear C++ gurus, I'm having some problems inserting some template classes into containers. I'm looking for ideas, suggestions,... on how to achieve this but first let me explain my problem....
4
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
2
by: bigfaceworm | last post by:
I had what I thought was proper SFINAE code. Two declarations of a function, one more general, one more specific. The code compiles just fine on gcc 3.3, but fails on 3.4 and 3.4.2. What is...
3
by: bonj | last post by:
Hello How do I declare a template class such that the template class T must implement a certain interface? e.g. interface IProvideID { int MyID; };
5
by: Amit | last post by:
Greetings all, I am writing some code somehwat similar to the test code I have below. I am having a variety of issues with template specialization. I am not sure if this is related to something...
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...
2
by: shuisheng | last post by:
Dear All, Assume I have a class template: template<class T, int N = 0> class A { void fun1(); void fun2(); void fun3();
6
by: subramanian100in | last post by:
consider the following program: #include <iostream> #include <cstdlib> using namespace std; template<typename Tvoid fcn(T arg) { cout << "from fcn(T arg)" << endl;
4
by: abir | last post by:
I am matching a template, and specializing based of a template, rather than a single class. The codes are like, template<template<typename T,typename Alloc = std::allocator<T> class pix{ }; ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
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
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...
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,...
0
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...

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.