Using default argument in the template function. - c++

This is a discussion on Using default argument in the template function. - c++ ; Hi folks, I am trying to add a default argument to a template function in a template class, here is the code snippet: template<typename T> class Test { template<typename U> friend void doSomething(U level); }; template<typename U> void doSomething(U level ...

+ Reply to Thread
Results 1 to 3 of 3

Using default argument in the template function.

  1. Default Using default argument in the template function.

    Hi folks,

    I am trying to add a default argument to a template function in a
    template class, here is the code snippet:

    template<typename T>
    class Test {
    template<typename U>
    friend void doSomething(U level);
    };

    template<typename U>
    void doSomething(U level = 0) {
    cout << "Here I am doing something\n";
    }

    int main() {
    doSomething();
    }

    An error araise at the compile time:

    error C2783: 'void doSomething(U)' : could not deduce template
    argument for 'U'

    How can I define such a template function taking a default argument?
    Thank you.


  2. Default Re: Using default argument in the template function.

    Αυκ» wrote:
    > Hi folks,
    >
    > I am trying to add a default argument to a template function in a
    > template class, here is the code snippet:
    >
    > template<typename T>
    > class Test {
    > template<typename U>
    > friend void doSomething(U level);
    > };
    >
    > template<typename U>
    > void doSomething(U level = 0) {
    > cout << "Here I am doing something\n";
    > }
    >
    > int main() {
    > doSomething();
    > }
    >
    > An error araise at the compile time:
    >
    > error C2783: 'void doSomething(U)' : could not deduce template
    > argument for 'U'
    >
    > How can I define such a template function taking a default argument?
    > Thank you.
    >


    The actual argument passed into the function is used to instantiate the
    function template, so if you really want to do in the default argument
    way, I think you have explicitly write the template param, like the
    following:

    doSomething<your type>();

  3. Default Re: Using default argument in the template function.

    On 8 1 , 12 20 , <leomayleo...> wrote:
    > Hi folks,
    >
    > I am trying to add a default argument to a template function in a
    > template class, here is the code snippet:
    >
    > template<typename T>
    > class Test {
    > template<typename U>
    > friend void doSomething(U level);
    >
    > };
    >
    > template<typename U>
    > void doSomething(U level = 0) {
    > cout << "Here I am doing something\n";
    >
    > }
    >
    > int main() {
    > doSomething();
    >
    > }

    dosomething<yourtype>();



+ Reply to Thread

Similar Threads

  1. Default template argument problem
    By Application Development in forum c++
    Replies: 1
    Last Post: 11-01-2007, 10:01 AM
  2. argument deduction for function template
    By Application Development in forum c++
    Replies: 2
    Last Post: 06-22-2007, 03:25 PM
  3. argument deduction for function template
    By Application Development in forum c++
    Replies: 8
    Last Post: 06-21-2007, 01:56 PM
  4. Template Function use as function argument problem,thanks.
    By Application Development in forum c++
    Replies: 3
    Last Post: 06-14-2007, 05:50 AM
  5. Function template argument deduction for string literals
    By Application Development in forum c++
    Replies: 3
    Last Post: 01-26-2007, 12:00 PM