Export (0) Print
Expand All
Expand Minimize

Compiler Warning (level 1) C4519

default template arguments are only allowed on a class template

Default template arguments are allowed only on a class template declaration or definition. Default template arguments may not be used in a function template, or in the definition of a member of a class template.

By default, C4519 is issued as an error.

The following code shows how to use default template arguments, shows some common mistakes, and shows how to issue C4519 as a warning.

// C4519.cpp
// compile with: /W1
// C4519 expected
#pragma warning(1 : 4519)
#include <stdio.h>

// OK : default template argument is allowed on a class template
template<class T=int> class A
{
public:
    static T f(T a, T b) 
    { 
        return a + b; 
    };
};

class B
{
public:
    // C4519 : default template argument is not allowed here
    template<class T=int> static T f(T a, T b) 
    { 
        return a + b; 
    };
};

// C4519 : default template argument is not allowed here
template<class T=int> T f(T a, T b) 
{ 
    return a + b; 
};

int main()
{
    double a = 3.5, b = 4.25;
    double d = 0.0;

    // Double is deduced from the types of the arguments to f().
    // Note that f's default template argument is ignored.
    d = f(a, b);   // double f(double, double)

    // Prints the value 7.75
    printf_s("%2.2lf\n", d);

    // Template argument defaults to int. Arguments are
    // coerced to ints, and an int is returned.
    d = A<>::f(a, b);        // int A<>::f(int, int)

    printf_s("%2.2lf\n", d);   // Prints the value 7.00

    // Double is deduced from the types of the
    // arguments to B::f(). Note that B::f's default
    d = B::f(a, b);

    // template argument is ignored.
    printf_s("%2.2lf\n", d);   // Prints the value 7.75
}

Community Additions

ADD
Show:
© 2014 Microsoft