CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: template class with template members

  1. #1
    Join Date
    Feb 2015
    Posts
    2

    template class with template members

    I have a class like this

    PHP Code:
    template<class X>
    class 
    A
    {
      
    X m_x;
    public:
        
    Xfoo();
        
    Xbar();
        
    //others are not related to X
    }; 
    I would like to get rid of
    PHP Code:
    template<class X
    for class level but still use it for members. Like this
    PHP Code:
    class A
    {
      
    X m_x;
    public:
        
    template<class X>
        
    Xfoo();
        
    template<class X>
        
    Xbar();
        
    //others are not related to X
    }; 
    However, I am still stuck at
    PHP Code:
    X m_x

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: template class with template members

    You cannot do that since m_x is a member variable, so its type must be available at that point.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Feb 2015
    Posts
    1

    Re: template class with template members

    You can use void* to get rid of the outer template
    Code:
    class A
    {
      void* m_x;
    public:
        void* foo();
        void* bar();
        //others are not related to X
    };

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,154

    Re: template class with template members

    Yuk!!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.3)

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: template class with template members

    if you make X a member of A then the X class needs to be fully defined by the time the compiler tries to compile the A class.

    if you make a pointer to X or a reference to X member, then you only need a forward reference to X, and the actual definition of X can come later.

    you don't need a template function to return an X*, a forward is enough. You will probably need the full X definition when implementing said function though.

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: template class with template members

    Quote Originally Posted by BenchMark View Post
    You can use void* to get rid of the outer template
    Code:
    class A
    {
      void* m_x;
    public:
        void* foo();
        void* bar();
        //others are not related to X
    };
    That's not really a solution. How will this code be able to create/copy/destroy an instance of X?

    @Tiny Dolls
    Why do you want to get rid of the class template? What do you want to accomplish at a higher level?
    If I had to guess, I'd guess you want to implement some kind of type erasure.
    ...but I don't like guessing, so I won't elaborate unless I know it's worth the effort.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    Join Date
    Feb 2015
    Posts
    2

    Re: template class with template members

    Quote Originally Posted by D_Drmmr View Post
    That's not really a solution. How will this code be able to create/copy/destroy an instance of X?

    @Tiny Dolls
    Why do you want to get rid of the class template? What do you want to accomplish at a higher level?
    If I had to guess, I'd guess you want to implement some kind of type erasure.
    ...but I don't like guessing, so I won't elaborate unless I know it's worth the effort.
    Yes I think this is what I am looking for.
    I also tried what Benchmark suggested and it compilable. But as you mentioned I am wondering how coding like this would result in anything bad
    PHP Code:
    voidpv=(list<string>)GetIPList();
    //something very long processed and processing
    list<stringret=pv;
    return 
    ret

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)