Define pointer to member as a template

Discussion in 'C++' started by ds, Apr 16, 2008.

  1. ds

    ds Guest

    Hi all,

    what I try to do is the following:

    template<class Tp> class themap
    {
    public:

    typedef int Tp::*ptr;
    static std::map<std::string, Tp::ptr> smap;
    };
    template<class Tp>
    std::map<std::string, Tp::ptr> themap<Tp>::smap;

    though the typedef compiles, the specialization of the map fails. The
    idea is to make it possible to define a pointer to an integer member
    of a class and use it in the mappings. Unfortunately I cannot find any
    reasonable workaround apart from declaring the typedef and the map in
    every class separately. Any ideas?

    thanks a lot!

    -- dimitris
     
    ds, Apr 16, 2008
    #1
    1. Advertising

  2. ds a écrit :

    > template<class Tp>
    > std::map<std::string, Tp::ptr> themap<Tp>::smap;
    >
    > though the typedef compiles, the specialization of the map fails.


    You have to indicate that Tp::ptr is a type.

    template<class Tp>
    std::map<std::string, typename Tp::ptr> themap<Tp>::smap;

    Michael
     
    Michael DOUBEZ, Apr 16, 2008
    #2
    1. Advertising

  3. ds

    ds Guest

    On Apr 16, 12:38 pm, Michael DOUBEZ <> wrote:
    > ds a écrit :
    >
    > > template<class Tp>
    > > std::map<std::string, Tp::ptr> themap<Tp>::smap;

    >
    > > though the typedef compiles, the specialization of the map fails.

    >
    > You have to indicate that Tp::ptr is a type.
    >
    > template<class Tp>
    > std::map<std::string, typename Tp::ptr> themap<Tp>::smap;
    >
    > Michael


    Hi Michael,

    thanks for the reply. You would be correct if I did not actually
    define the type! The problem is that

    typedef int Tp::*ptr; defines a pointer to integer members of class
    Tp. This line compiles fine as well. However, the next line (std::map
    memberr) results in

    'std::map' : 'Tp::ptr' is not a valid template type argument for
    parameter '_Ty' on MSVC.

    If I use the typename in the declaration and instantiation of the map
    and then typedef the pointer in my classes, like in the following:

    template<class Tp> class themap
    {
    public:
    static std::map<std::string, typename Tp::ptr> smap;
    };
    template<class Tp>
    std::map<std::string,typename Tp::ptr> themap<Tp>::smap;

    class test : public themap<test>
    {
    public:
    int a;
    int b;
    typedef int test::*ptr;
    };

    I get 'ptr' : is not a member of 'test'... plus that the point is to
    have the typedef in the template.

    Thanks a lot!
     
    ds, Apr 16, 2008
    #3
  4. ds a écrit :
    > On Apr 16, 12:38 pm, Michael DOUBEZ <> wrote:
    >> ds a écrit :
    >>> though the typedef compiles, the specialization of the map fails.

    >> You have to indicate that Tp::ptr is a type.

    [snip]
    > thanks for the reply. You would be correct if I did not actually
    > define the type!

    [snip]
    > template<class Tp> class themap
    > {
    > public:
    > static std::map<std::string, typename Tp::ptr> smap;
    > };
    > template<class Tp>
    > std::map<std::string,typename Tp::ptr> themap<Tp>::smap;


    Yes, typename should be used in themap<> also.


    > class test : public themap<test>
    > {
    > public:
    > int a;
    > int b;
    > typedef int test::*ptr;
    > };
    >
    > I get 'ptr' : is not a member of 'test'... plus that the point is to
    > have the typedef in the template.


    Yes, you cannot use Tp:: in themap. Only in functions otherwise you have
    a circularity in the definition: themap<test>::ptr must be defined to
    define test and test must be defined to define themap<test>::ptr.

    The CRTP works only with functions.

    Michael
     
    Michael DOUBEZ, Apr 16, 2008
    #4
  5. ds

    ds Guest

    On Apr 16, 1:07 pm, Michael DOUBEZ <> wrote:
    > ds a écrit :
    >
    > > On Apr 16, 12:38 pm, Michael DOUBEZ <> wrote:
    > >> ds a écrit :
    > >>> though the typedef compiles, the specialization of the map fails.
    > >> You have to indicate that Tp::ptr is a type.

    > [snip]
    > > thanks for the reply. You would be correct if I did not actually
    > > define the type!

    > [snip]
    > > template<class Tp> class themap
    > > {
    > > public:
    > > static std::map<std::string, typename Tp::ptr> smap;
    > > };
    > > template<class Tp>
    > > std::map<std::string,typename Tp::ptr> themap<Tp>::smap;

    >
    > Yes, typename should be used in themap<> also.
    >
    > > class test : public themap<test>
    > > {
    > > public:
    > > int a;
    > > int b;
    > > typedef int test::*ptr;
    > > };

    >
    > > I get 'ptr' : is not a member of 'test'... plus that the point is to
    > > have the typedef in the template.

    >
    > Yes, you cannot use Tp:: in themap. Only in functions otherwise you have
    > a circularity in the definition: themap<test>::ptr must be defined to
    > define test and test must be defined to define themap<test>::ptr.
    >
    > The CRTP works only with functions.
    >
    > Michael


    Hi again Michael and thanks a lot for the clarifications. Though I
    risk to become overly stubborn and besides the fact that I somehow get
    the feeling that this cannot be done, I am not sure if this is
    actually a conceptual error or a language limitation like template
    typedefs. My original template should instantiate to

    template<test> class themap
    {
    public:

    typedef int test::*ptr;
    static std::map<std::string, test::ptr> smap;
    };

    However the typedef provides only an alias to the real name and does
    not define a new type as I would like and I think that this is rather
    the problem. Anyway, thanks for the feedback!
     
    ds, Apr 16, 2008
    #5
    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. Fraser Ross
    Replies:
    4
    Views:
    1,097
    Fraser Ross
    Aug 14, 2004
  2. robin liu
    Replies:
    3
    Views:
    866
    Robin Liu
    Apr 21, 2006
  3. Stephen Howe
    Replies:
    2
    Views:
    308
    Stephen Howe
    Nov 6, 2012
  4. somenath
    Replies:
    10
    Views:
    314
    James Kanze
    Jul 2, 2013
  5. somenath
    Replies:
    2
    Views:
    179
    somenath
    Aug 29, 2013
Loading...

Share This Page