MSDN Forum: Splitting templated function with default argument into declaration + definition gives compilation error - by CSSForumEngineer

Status : 

 


2
0
Sign in
to vote
ID 496593 Comments
Status Active Workarounds
Type Bug Repros 0
Opened 10/8/2009 3:17:56 AM
Access Restriction Public

Description

http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/762928c4-8361-44d5-9544-8f2e569a47b9

I have strange situation involving a templated function with a default
argument.  The strange thing is that it compiles fine if I define the
function directly, but gives a compilation error with VC++ 2005 if I
first declar the function.

This works:

     #include <limits>

    template<typename T>
    struct C {
      typedef T Type;
    };

    template <typename T>
    void f(typename T::Type x =
             std::numeric_limits<typename T::Type>::max()) {}

    void use() {
      f<C<int> >();
    } 

Changing the direct definition of f above into a declaration plus a
definition as shown below gives a compilation error:
  
     template <typename T>
    void f(typename T::Type x =
             std::numeric_limits<typename T::Type>::max()); // FAILS

    template <typename T>
    void f(typename T::Type x) {}
             
    error C2589: '::' : illegal token on right side of '::'
    error C2059: syntax error : '::' 

Also strange is that if I replace the use of numeric_limits with the
following, which I think should be equivalent, I get a different
error:

     template<typename T>
    struct D {
      static T bar() throw() { return 1; }
    };

    template <typename T>
    void f(typename T::Type x = D<typename T::Type>::bar()); // FAILS

    error C2064: term does not evaluate to a function taking 0 arguments 

Am I missing something or is this a bug in the compiler?  (The code
works on G++ and with Comeau C++.)
Sign in to post a comment.
Posted by Microsoft on 10/15/2009 at 2:05 PM
Hello CSSForumEngineer,

Thank you for submitting this feedback. While we recognize that this is a valid compiler bug, it is below our triage bar at this point in the product cycle. The workaround is to define the template function where you have declared it. If you are concerned about the perf impact of recompiling the template function for each translation unit, using PCH files should eliminate this overhead.

Thanks,
Mark Roberts
Visual C++ Team