How to make this template function work with VC 6?

Posted on 2005-04-05
Last Modified: 2008-03-04
#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?
Question by:luoys
    LVL 86

    Expert Comment

    >>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.
    LVL 86

    Accepted Solution

    BTW, see also;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 (
    LVL 8

    Expert Comment

    This should get you through for now: ["Visual C++ 2005 Express Beta"]

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


    Author Comment

    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.
    LVL 8

    Expert Comment

    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.

    LVL 30

    Expert Comment

    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 :-)
    LVL 48

    Expert Comment

    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.
    LVL 39

    Expert Comment

    >>>> 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 Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now