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

Error compiling template function within class template

template <class T>
class foo
{
public:
template <class Tin>
T bar (Tin) {return T();}
};

class derived : public foo<derived>
{
};

Some compilers compile successfully, while another tells me that I'm
using the undefined class 'derived'.

Is this little chunk of code compliant with the ISO C++ standard or not?

Jul 22 '05 #1
4 2118
uv******@yahoo.com wrote:
template <class T>
class foo
{
public:
template <class Tin>
T bar (Tin) {return T();}
};

class derived : public foo<derived>
{
};

Some compilers compile successfully, while another tells me that I'm
using the undefined class 'derived'.

Is this little chunk of code compliant with the ISO C++ standard or not?


It probably is in that grey area, you know, twilight zone, where
all the templates live before they pop up into our minds...

The code is compliant. By the time you get around to initialising
a 'derived' object, the class 'derived' and therefore its base class
'foo<derived>' will have been completely defined. The compilers that
can't handle that kind of code aren't sophisticated enough to delay
generating of the code until it's actually needed.

Of course, you could have written

class derived : public foo<derived>
{
derived fubar() {
return foo<derived>::template bar(42);
}
};

which would cause an attempt to use 'foo<derived>' _before_ 'derived'
was completely defined. In that case the work-around is to place the
body of 'derived::fubar' outside the class definition.

V
Jul 22 '05 #2
Victor Bazarov wrote in
news:FG*****************@newsread1.dllstx09.us.to. verio.net in
comp.lang.c++:
uv******@yahoo.com wrote:
template <class T>
class foo
{
public:
template <class Tin>
T bar (Tin) {return T();}
};

class derived : public foo<derived>
{
};

Some compilers compile successfully, while another tells me that I'm
using the undefined class 'derived'.

Is this little chunk of code compliant with the ISO C++ standard or
not?


It probably is in that grey area, you know, twilight zone, where
all the templates live before they pop up into our minds...

The code is compliant. By the time you get around to initialising
a 'derived' object, the class 'derived' and therefore its base class
'foo<derived>' will have been completely defined. The compilers that
can't handle that kind of code aren't sophisticated enough to delay
generating of the code until it's actually needed.

Of course, you could have written

class derived : public foo<derived>
{
derived fubar() {
return foo<derived>::template bar(42);
}
};

which would cause an attempt to use 'foo<derived>' _before_ 'derived'
was completely defined. In that case the work-around is to place the
body of 'derived::fubar' outside the class definition.


I tried it and the workaround wasn't needed, however with VC 7.1,
which exhibits the original problem, moving the member defenition
out of foo was required:

template < typename T > template < typename Tin >
T foo< T >::bar( Tin )
{
return T();
}

Incresing I'm thinking that MSVC 7.1 is pants.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
Rob Williscroft wrote:
[...]
Incresing I'm thinking that MSVC 7.1 is pants.


Rob, sorry for an off-topic question, but I am not familiar with
that idiom. Could you translate your sentence for me, please.
What does it mean "to be pants"? I found a verb "to [de]pants"
but that must not be it. Or is it? Thanks. -- Victor
Jul 22 '05 #4
Victor Bazarov wrote in news:aA4nd.11122$Ae.10475
@newsread1.dllstx09.us.to.verio.net in comp.lang.c++:
Rob Williscroft wrote:
[...]
Incresing I'm thinking that MSVC 7.1 is pants.


Rob, sorry for an off-topic question, but I am not familiar with
that idiom. Could you translate your sentence for me, please.
What does it mean "to be pants"? I found a verb "to [de]pants"
but that must not be it. Or is it? Thanks. -- Victor


LOL, sorry about that, it means rubbish.

http://www.proz.com/?sp=h&id=651556

I'll admit I thought it was a Bart (Simpsons) -ism, so genuine
International English, but apparently its very British.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5

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

Similar topics

2
by: uvts_cvs | last post by:
template <class T> class foo { public: template <class Tin> T bar (Tin) {return T();} };
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
1
by: Karine Pinault | last post by:
I am having trouble trying to compile existing code with the /clr flag. The following error occurs and I can't figure it out how to fix this problem. I have the following declaration:...
2
by: David Williams | last post by:
Hi all, I get errors when compiling the .cpp file below, both on Visual Studio and G++. 01: //------------------------------------------------- 02: template <typename Type> 03: class Outer 04:...
6
by: Yan | last post by:
Here is the code: class A {}; void (A::*A) (); // Line 3 int main() { A a; // Line 6 return 0; }
3
by: hyd | last post by:
Hello, I use VS2005 - C++/CLI. I have some kind of events in native C++. I want to raise .NET events when my native C++ events occur. So, I wrote some code for that but I have an C1001 error (An...
2
by: Nick | last post by:
I'm learning C++ and ran into a compile error using Visual C++ 2005 Express on the following example program (located at http://www.cplusplus.com/doc/tutorial/templates.html): // template...
7
by: JustBeSimple | last post by:
Hi Everyone, I'm having a problem compiling useing the VS2005 .NET I need help to resolve those error, I try to create a new project it doesn't help any suggestion? I got the following errors:...
1
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I...
6
by: hsmit.home | last post by:
Hello, I came across a strange error and it's really been bugging me. Maybe someone else has come across this and any insight would be appreciated. What I'm trying to accomplish is using...
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: 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: 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
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...

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.