Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Why non-template function does not compile where as template function compiles?

Why non-template function does not compile where as template function compiles?

Scheduled Pinned Locked Moved ATL / WTL / STL
questionhelpannouncement
3 Posts 3 Posters 13 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PBMBJoshi
    wrote on last edited by
    #1

    I am trying to call two identical functions (one is templated and the other one is normal). The templated one compiles successfully, but the non-templated one gives compilation error. Why?

    template
    void Template_universal_ref(ParamType&& param)
    {
    cout << "Template_universal_ref, param : " << param << endl;
    }

    void NonTemplate_universal_ref(int&& param)
    {
    cout << "NonTemplate_universal_ref, param : " << param << endl;
    }

    int main()
    {
    int x=5;
    Template_universal_ref(x);
    //NonTemplate_universal_ref(x); // This is giving compilation error
    return 0
    }

    In my view

    NonTemplate_universal_ref(x);

    is special version of

    Template_universal_ref(x);

    . Why does NonTemplate_universal_ref(x); result in compilation error?

    L K 2 Replies Last reply
    0
    • P PBMBJoshi

      I am trying to call two identical functions (one is templated and the other one is normal). The templated one compiles successfully, but the non-templated one gives compilation error. Why?

      template
      void Template_universal_ref(ParamType&& param)
      {
      cout << "Template_universal_ref, param : " << param << endl;
      }

      void NonTemplate_universal_ref(int&& param)
      {
      cout << "NonTemplate_universal_ref, param : " << param << endl;
      }

      int main()
      {
      int x=5;
      Template_universal_ref(x);
      //NonTemplate_universal_ref(x); // This is giving compilation error
      return 0
      }

      In my view

      NonTemplate_universal_ref(x);

      is special version of

      Template_universal_ref(x);

      . Why does NonTemplate_universal_ref(x); result in compilation error?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The double ampersand in the non-template version is illegal. Change it to a single one for a reference parameter, or none at all for a direct value.

      1 Reply Last reply
      0
      • P PBMBJoshi

        I am trying to call two identical functions (one is templated and the other one is normal). The templated one compiles successfully, but the non-templated one gives compilation error. Why?

        template
        void Template_universal_ref(ParamType&& param)
        {
        cout << "Template_universal_ref, param : " << param << endl;
        }

        void NonTemplate_universal_ref(int&& param)
        {
        cout << "NonTemplate_universal_ref, param : " << param << endl;
        }

        int main()
        {
        int x=5;
        Template_universal_ref(x);
        //NonTemplate_universal_ref(x); // This is giving compilation error
        return 0
        }

        In my view

        NonTemplate_universal_ref(x);

        is special version of

        Template_universal_ref(x);

        . Why does NonTemplate_universal_ref(x); result in compilation error?

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #3

        int&& is a rvalue reference. Until C++11 we only had lvalue references. In Nontemplate_universal_ref(x), x is a lvalue, not an rvalue. Try the following:

        template void Template_universal_ref(ParamType&& param)
        {
        cout << "Template_universal_ref, param : " << param << endl;
        }

        void NonTemplate_universal_ref(int&& param)
        {
        cout << "NonTemplate_universal_ref(int&&), param : " << param << endl;
        }

        void NonTemplate_universal_ref(int& param)
        {
        cout << "NonTemplate_universal_ref(int&), param : " << param << endl;
        }

        int main()
        {
        int x=5;
        Template_universal_ref(x);
        NonTemplate_universal_ref(x);
        NonTemplate_universal_ref(x+3);
        return 0;
        }

        Note that in the second call to NonTemplate_universal_ref, the argument x+3 is not an lvalue, that is, it cannot be assigned to. Now, maybe someone can explain why the call via the template works?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups