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. C / C++ / MFC
  4. Member to pointer as template argument

Member to pointer as template argument

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
5 Posts 3 Posters 0 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.
  • L Offline
    L Offline
    LionAM
    wrote on last edited by
    #1

    Hello, is there a convenient way to pass a pointer to a data member so that it is recognized as a template argument? The following does not compile under VC++ 2008:

    template
    void AddMember(Type Parent::*Member)
    {
    return true;
    }

    ...

    AddMember(&TestObject::i);
    //error C2783: could not deduce template argument "Member"

    Alex

    A _ 2 Replies Last reply
    0
    • L LionAM

      Hello, is there a convenient way to pass a pointer to a data member so that it is recognized as a template argument? The following does not compile under VC++ 2008:

      template
      void AddMember(Type Parent::*Member)
      {
      return true;
      }

      ...

      AddMember(&TestObject::i);
      //error C2783: could not deduce template argument "Member"

      Alex

      A Offline
      A Offline
      Albert Holguin
      wrote on last edited by
      #2

      Not completely sure... but did you try just this:

      void AddMember(Type *Member)

      If you're calling a method for it, no need to specify Parent::*Member, where the pointer comes from is up to the caller.

      1 Reply Last reply
      0
      • L LionAM

        Hello, is there a convenient way to pass a pointer to a data member so that it is recognized as a template argument? The following does not compile under VC++ 2008:

        template
        void AddMember(Type Parent::*Member)
        {
        return true;
        }

        ...

        AddMember(&TestObject::i);
        //error C2783: could not deduce template argument "Member"

        Alex

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        Try this -

        template<class Parent, typename Type>
        void AddMember(Type Parent::*Member)
        {
        return true;
        }

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++)

        Polymorphism in C

        L 1 Reply Last reply
        0
        • _ _Superman_

          Try this -

          template<class Parent, typename Type>
          void AddMember(Type Parent::*Member)
          {
          return true;
          }

          «_Superman_»  _I love work. It gives me something to do between weekends.

          _Microsoft MVP (Visual C++)

          Polymorphism in C

          L Offline
          L Offline
          LionAM
          wrote on last edited by
          #4

          Thank you for your answers. Please look at this example:

          struct TestObject
          {
          int i;
          };

          template
          void test() {}

          ...

          test<&TestObject::i>();

          This way, I can call the function template so that the member pointer is passed as a (non-type) template argument. However, in my case, I would like that parent and type are template parameters, too:

          struct TestObject
          {
          int i;
          };

          template
          void test() {}

          ...

          test();

          However, this is not very convenient to call test() as you have to specify all template arguments manually. If you pass &TestObj::i as a argument to the function, Parent and Type are deduced - but not the Member... Alex

          _ 1 Reply Last reply
          0
          • L LionAM

            Thank you for your answers. Please look at this example:

            struct TestObject
            {
            int i;
            };

            template
            void test() {}

            ...

            test<&TestObject::i>();

            This way, I can call the function template so that the member pointer is passed as a (non-type) template argument. However, in my case, I would like that parent and type are template parameters, too:

            struct TestObject
            {
            int i;
            };

            template
            void test() {}

            ...

            test();

            However, this is not very convenient to call test() as you have to specify all template arguments manually. If you pass &TestObj::i as a argument to the function, Parent and Type are deduced - but not the Member... Alex

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            I understand that you want the parent and type to be template parameters. So let me repeat my earlier code snippet -

            template<class Parent, typename Type>
            void AddMember(Type Parent::*Member)
            {
            return true;
            }

            Here Parent and Type are template parameters and the calling is also convenient - AddMember(&TestObject::i); Here Parent will be deduced to TestObject and Type will be deduced to int. And Member will point to i. I think this is as convenient as it can get.

            «_Superman_»  _I love work. It gives me something to do between weekends.

            _Microsoft MVP (Visual C++)

            Polymorphism in C

            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