Home Dashboard Directory Help
Search

compile syntax error for template function specialization with default argument by rvx1024


Status: 

Closed
 as Fixed Help for as Fixed


4
0
Sign in
to vote
Type: Bug
ID: 583081
Opened: 8/3/2010 8:39:50 PM
Access Restriction: Public
0
Workaround(s)
view
2
User(s) can reproduce this bug

Description

problem compiling these codes:
        #include <limits>
    template <typename T> void function(T arg1,
        T min = std::numeric_limits<T>::min(),
        T max = std::numeric_limits<T>::max())
    {
    }

    template <> void function<int>(int arg1, int min,int max)
    {
    }

    int main(int argc,char* argv[])
    {
        function<int>(1);
    }

it give syntax error C2689 and C2059 on function default argument line on :: token. but without specialization, it doing fine. and if I change default argument and still doing specialization:

    template <typename T> void function(T arg1,
        T min = T(0),
        T max = T(1))
    {
    }
    template <> void function<int>(int arg1, int min,int max)
    {
    }

the problem gone too.

now if I use it like this: function<int>(1,2,3); or function<float>(1.0f) its fine, so it seems that if template function is specialized, we must rewrite the default argument when call it.

but on my second case, where i replace std::numeric_limits<T>::.. with T(..) there no syntax error when calling function<int>(1).
Details
Sign in to post a comment.
Posted by Microsoft on 9/9/2010 at 4:25 PM
Hi -

Thanks for filing this issue. It is definitely a bug in our compiler - in particular there are a couple of really nasty ones around default template parameters. The workaround here is to explicitly specify the template parameters. Unfortunately, fixing this area of our compiler is costly, prone to introducing more severe issues, and may cause issues in existing (malformed but operational) code. Given this, we are planning not to fix this bug for our next release, in favor of more severe issues for which there is no workaround.

Thanks,
Andy Rich
Visual C++ QA
Posted by James McNellis on 8/4/2010 at 6:27 PM
The primary template must have a parameter that has a default argument value. The default argument value must be a member function of a class template not in the global namespace.

The following is minimal code to reproduce:

    namespace N
    {
        template <typename T>
        struct S
        {
            static T g() { return T(); }
        };
    }

    template <typename T> void f(T = N::S<T>::g()) { }

    template <> void f<>(int) { }

    int main()
    {
        f<int>();
    }

The compiler emits the following errors, both on the line where the primary template is defined:

    error C2589: '::' : illegal token on right side of '::'
    error C2059: syntax error : '::'

Interestingly, there is another issue if the class template is in the global namespace. Given the following code:

    template <typename T>
    struct S
    {
        static T g() { return T(); }
    };

    template <typename T> void f(T = ::S<T>::g()) { }

    template <> void f<>(int) { }

    int main()
    {
        f<int>();
    }

The compiler emits the following error on the line on which the primary template is defined:

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

Both of these example test cases are well-formed C++ programs.

These issues are similar to another issue I reported in May, which was closed as Won't Fix: "Visual C++ fails to compile member function with function template called as default argument:"

    https://connect.microsoft.com/VisualStudio/feedback/details/557653/
Posted by Microsoft on 8/4/2010 at 2:46 AM
Thank you for reporting the issue.
We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution.These specialized experts will follow-up with your issue.
Sign in to post a workaround.