Why template by return value is forbidden?

Discussion in 'C++' started by PLM, Aug 10, 2005.

  1. PLM

    PLM Guest

    Hello, all.
    I should lke to declare template function as following:

    template <class TYPE>
    TYPE Test() {return (TYPE)5; }

    void main(void) {
    int i; float f;
    i = Func();
    f = Func();
    }

    What is wrong with this?
    What I receive from gcc is :

    main.cpp:28: no matching function for call to `Func()'


    Thanks ahead
    Leon
     
    PLM, Aug 10, 2005
    #1
    1. Advertising

  2. PLM

    msalters Guest

    PLM schreef:

    > Hello, all.
    > I should lke to declare template function as following:
    >
    > template <class TYPE>
    > TYPE Func() {return (TYPE)5; } // c/p error fixed - MSA
    >
    > void main(void) {
    > int i; float f;
    > i = Func();
    > f = Func();
    > }
    >
    > What is wrong with this?
    > What I receive from gcc is :
    >
    > main.cpp:28: no matching function for call to `Func()'


    The definition is completely legal. However, you must specify
    TYPE. You simply cannot deduce TYPE from the =, because the overload
    of operator= is selected based on TYPE. C++ resolves these things
    from the bottom up. One has to choose a direction when breaking
    such logical cycles.

    So, write i = Func<int>(); or
    template <class TYPE>
    void Func( TYPE& t ) { t = 5; }
    Func( i );

    HTH,
    Michiel Salters
     
    msalters, Aug 10, 2005
    #2
    1. Advertising

  3. Hi,

    of course, I suppose that you meant to call Test(), since Func() is not
    declared.

    Template matching is based on function overloading matching.

    Function overloading doesn't work for return types, but only for
    parameter's types.

    You have to specify the template specialization like this:

    i = Func<int>();

    Ciao,
    Mario Fratelli.
     
    Mario Fratelli, Aug 10, 2005
    #3
  4. PLM

    PLM Guest

    msalters wrote:

    >
    > PLM schreef:
    >
    >> Hello, all.
    >> I should lke to declare template function as following:
    >>
    >> template <class TYPE>
    >> TYPE Func() {return (TYPE)5; } // c/p error fixed - MSA
    >>
    >> void main(void) {
    >> int i; float f;
    >> i = Func();
    >> f = Func();
    >> }
    >>
    >> What is wrong with this?
    >> What I receive from gcc is :
    >>
    >> main.cpp:28: no matching function for call to `Func()'

    >
    > The definition is completely legal. However, you must specify
    > TYPE. You simply cannot deduce TYPE from the =, because the overload
    > of operator= is selected based on TYPE. C++ resolves these things
    > from the bottom up. One has to choose a direction when breaking
    > such logical cycles.
    >
    > So, write i = Func<int>(); or
    > template <class TYPE>
    > void Func( TYPE& t ) { t = 5; }
    > Func( i );
    >
    > HTH,
    > Michiel Salters

    Thank you for reply.
    Yes, I understand that i = Func<int>() will work, but this is exactly I
    wanted to avoid...:))
    Now, please, can you be so kind to detail your explanation about the
    direction? I did not catch...:-((
     
    PLM, Aug 10, 2005
    #4
  5. PLM

    benben Guest

    > Yes, I understand that i = Func<int>() will work, but this is exactly I
    > wanted to avoid...:))


    Why do you want to avoid it? If Func is to return an object of some type,
    then you need to specify what that type is. It is only when that is
    determined the compiler would worry about how to convert the return type for
    the assignment operation.

    One way to work around is to not use the return value mechanism. Pass the
    variable by reference:

    template <typename T>
    Func(T& t)
    {
    t = T(5);
    }

    > Now, please, can you be so kind to detail your explanation about the
    > direction? I did not catch...:-((
    >


    Consider:

    class A{};
    class B{};

    A& operator = (A&, const B&);
    B& operator = (B&, const A&);

    template <typename T>
    T Func(void);

    A a;
    a = Func(); // problem line

    In the last line (commented problem line), the compiler has serveral
    options, neither is much better than the other and thus would raise
    ambiguity. In fact, the compiler never knows ahead about the conversion so
    it simply can't depend on the return type to match functions (and function
    templates).
     
    benben, Aug 10, 2005
    #5
  6. PLM

    Mike Wahler Guest

    "PLM" <> wrote in message
    news:1123666994.a7cd07df2ba8be9f1c731a0d124d6dd4@teranews...
    > Hello, all.
    > I should lke to declare template function as following:
    >
    > template <class TYPE>
    > TYPE Test() {return (TYPE)5; }
    >
    > void main(void) {


    int main(void) {

    > int i; float f;
    > i = Func();
    > f = Func();'


    i = Test<int>();
    f = Test<float>();

    -Mike
     
    Mike Wahler, Aug 10, 2005
    #6
  7. I think template cast operator would help...
    Create a small class which encapsulates value of 5:

    static class {
    public:
    template<typename T>
    operator T() { return static_cast<T>(5); }
    } five = {};

    #include <iostream>
    using std::cout;
    using std::endl;

    int main() {
    float valuef = five;
    int valuei = five;

    cout << valuef << endl;
    cout << valuei << endl;

    return 0;
    }

    Hope that helps!
    Martin



    "PLM" <> wrote in message
    news:1123666994.a7cd07df2ba8be9f1c731a0d124d6dd4@teranews...
    > Hello, all.
    > I should lke to declare template function as following:
    >
    > template <class TYPE>
    > TYPE Test() {return (TYPE)5; }
    >
    > void main(void) {
    > int i; float f;
    > i = Func();
    > f = Func();
    > }
    >
    > What is wrong with this?
    > What I receive from gcc is :
    >
    > main.cpp:28: no matching function for call to `Func()'
    >
    >
    > Thanks ahead
    > Leon
    >
     
    Martin Vorbrodt, Aug 10, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Hunt
    Replies:
    4
    Views:
    11,385
    gullsinn
    Oct 4, 2009
  2. Douglas Reith
    Replies:
    11
    Views:
    1,126
    Micah Cowan
    Oct 1, 2003
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,231
    Smokey Grindel
    Dec 2, 2006
  4. Greenhorn
    Replies:
    15
    Views:
    884
    Keith Thompson
    Mar 6, 2005
  5. Mark J. McGinty

    IIS HTTP 403.1 Forbidden: Execute Access Forbidden

    Mark J. McGinty, Dec 9, 2005, in forum: ASP General
    Replies:
    2
    Views:
    388
    Kyle Peterson
    Dec 9, 2005
Loading...

Share This Page