473,396 Members | 1,760 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,396 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 2120
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...
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
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: 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
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
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.