Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

How to make this template function work with VC 6?

#include <iostream>
using namespace std;
template<int n> inline int power(int m) {
  return power<n-1>(m) * m;
template<int> inline int power<1>(int m) {
  return m;
template<int> inline int power<0>(int m) {
  return 1;

int main() {
      int m = 4;
      cout << power<3>(m) << endl;

Code is from "Thinking in C++" book. In VC6, the power<3>() function call will result the call to the second implementation power<1>, which seems obviously a bug in VC6. Anyway to get around it without significant refactoring the code?
1 Solution
>>Anyway to get around it without significant refactoring the code?

Yes. Get VC7.x, since VC6 unfortunately does not support partial template specialization at all.
BTW, see also http://support.microsoft.com/default.aspx?scid=kb;en-us;240866

You may get the following error message if you try to use class template partial specializations:
error C2989: 'A<T,2>' : template class has already been defined as a non-template class error C2988: unrecognizable template declaration/definition
Please refer to the sample in the More Information section for details.

The compiler does not support class template partial specializations as specified in the C++ Standard (
This should get you through for now: http://lab.msdn.microsoft.com/express/visualc/default.aspx ["Visual C++ 2005 Express Beta"]

Downloading the February 2005 Community Technical Preview Release is probably ok.

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

luoysAuthor Commented:
Thanks to every body... however I am limited to VC 6, this company will not upgrade the whole development environment just because of me.
Only recently I realize how dump the VC 6 is.
Well, you showed us an example from a book, but I'm sure you might have an actual solution in mind.  

If you describe that solution need, we might be able to come up with a work-around for you.

Hi luoys,
> >Only recently I realize how dump the VC 6 is.

VC++ 6.0 was developed before the official C++ standard was finalized.  Things that were added late to the standard are not in VC++6.0.
It's unfair to expect a pre-standard compiler to be fully compliant to a standard that did not exist when it was created.

Now if you say that VC++ 7.0 is a dumb compiler, then I would fully agree since this compiler is a post-standard compiler, and should have been more compliant to the standard.

Thankfully, MS manage to get it right with VC++ 7.1

David Maisonave :-)
Working with VC++ 6.0 you need to forget about such things, this is the only way.
power template calculates result at compile time. Instead of this make calculation at runtime. Or use calculator and set result to the program instead of using power template (this will produce the same result at run time).
If you are not library writer, you can successfully work in application development with VC++ 6.0 without all these tricks.
>>>> If you are not library writer, you can successfully work in application development
>>>> with VC++ 6.0 without all these tricks.

I agree to that - though I am a library writer.

VC 6 is the most used C++ compiler in the world and though VC 7.1 is better there are many developers that couldn't/wouldn't change as long as their current projects don't require that.

For the power functionality you need you could use the code below, that isn't as fancy as your approach but should do it's job.

int power(int n, int m)
     return (n <= 0)? 1 : (n == 1)? m : power(n-1, m);

Regards, Alex

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now