Template Return Type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gobi Sakthivel
    New Member
    • Jan 2013
    • 26

    #1

    Template Return Type

    Code:
    class xxx
    {
        public:
        template<class T>
         T void func(int a)
         {
            /* Here depending upon some-cases, i ll return                                       
             * either int, float, std::string, etc...
             * How to do it, i am getting undefined refernce  
             * to func
             */
        }
    };
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Firstly, this code:

    Code:
    T void func(int a)...
    shows a function with two return types. This won't work.

    Also, assume you have two func() functions generated from your template and one returns an int and the other returns a float. Then you code:

    Code:
    func(32);
    So does the compiler call func() that returns an int or func() that returns a float?

    The compiler won't guess. Instead it crashes the build with "indetermin ate behavior" and lets you figure out exactly what to do.

    Because of this scenario, function overloading in C++ does not apply to the return type only.
    Last edited by weaknessforcats; Aug 19 '13, 10:26 PM. Reason: typo

    Comment

    Working...