CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2001
    Posts
    391

    Question Function Templates within Class possible ??

    I've just discovered Function Templates and would like to implement it within my class. Is this possible?

    I've tried doing something like this:
    --
    class MyClass
    {
    public:
    template <class T> char WriteActionFile(T &incVar)
    }

    template <class T> char MyClass::WriteActionFile(T &incVar)
    {
    cout << sizeof T << endl;
    }

    void main(..)
    {
    MyClass myObj;
    long val=10;

    myObj.WriteActionFile(val);
    }
    --

    What happens is VC++6 returns back the error message:
    ==
    error C2893: Failed to specialize function template 'char __thiscall MyClass::WriteActionFile(T &)' - With the following template arguments:
    'char'
    ==

    Ok what did i do wrong? I've tried other combos but cant seem to get function templates to work for a specific class? Does this mean function templates can only be implemented within the global namespace? Ideally i would like to have the template prototype within the class declaration and the definition within the source file, that is, I would prefer not to have to inline the function body into the class declaration.
    Last edited by quantass; January 13th, 2003 at 01:20 AM.

  2. #2
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661
    i think that when you implement the function you shouldn't whrite

    template <class T>

    this would work

    char MyClass::WriteActionFile(T &incVar)
    {
    cout << sizeof T << endl;
    }
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  3. #3
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Bad news: Microsoft VC++ doesn't understand template member functions. They are in the Standard, though.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  4. #4
    Join Date
    Aug 2001
    Location
    Israel
    Posts
    101

    You can do it - no problem

    also in VC 6++. All you have to do, is impolement the function INLINE, like so:

    class MyClass
    {
    template <class X> int func (X x) { ..... }
    };

  5. #5
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Let's make it straight. VC++ will compile a class that has a template member function, but you will have a hard time to instantiate that member. The syntax you have to use in such a case is rather a rare one, and VC++ doesn't support it.
    Code:
    class A
    {
    public:
        template<typename T> 
            size_t get_size(){return sizeof(T);}
    };
    
    int main()
    {
        A a;
        size_t s = a.template get_size<int>(); //correct syntax, according to the Standard
        return 0; 
    }
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  6. #6
    Join Date
    Aug 2001
    Location
    Israel
    Posts
    101

    Not quite.

    This is only true in your example, since get_size doesn't have any arguments.

    If you run this code (compiled with VC++6):

    class C
    {
    public:
    template <class T>
    int getsize(const T& x) { return sizeof(x); }
    };

    struct S
    {
    int x,y,z;
    };

    int main(int argc, char* argv[])
    {
    C c;
    S s;
    printf("%d\n", c.getsize(1));
    printf("%d\n", c.getsize(1.5));
    printf("%d\n", c.getsize(s));
    return 0;
    }

    You'll get an output of:
    4
    8
    12

  7. #7
    Join Date
    Sep 2002
    Posts
    1,747
    Gabriel is correct in his assertion as to VC++'s support for template functions in general, however there are specific circumstances where function templates may be used. First, parametrized classes need only have their methods defined with the template parameter specified in the scope resolution, ie. something like
    Code:
    template <typename T>
    ReturnType SomeClass<T>::SomeMethod(param list)
    { /* Something that uses T */ }
    in the definition, and no parameter clarification is needed in the declaration (inside the class declaration).

    Secondly, parametrized functions themselves must use the template parameters in their parameter list. This Knowledge Base article explains this in more detail and gives the specific compiler workaround of dummy arguments.

    Finally, VC++ does not support general ordering of template specializations or even partial specializations (not applicable to parametrized functions anyway), and lookup may not be proper, so it is necessary to be careful when dealing with parametrized functions on the VC++ compiler to ensure proper behavior. Otherwise, for the specific OP problem, I would use the form I have given above in the code bracket...

    I hope this helps somewhat...

    [Edited the code snippet... silly me, I forgot the template keyword for name resolution...]
    Last edited by galathaea; January 13th, 2003 at 06:26 PM.
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  8. #8
    Join Date
    Aug 2001
    Location
    Israel
    Posts
    101
    The article you mentioned is EXACTLY my point. the 'get_size' member did not have any arguments nor a return value which is template-based, so obviously the compiler doesn't know how to instantiate it when it's called.

    Template specializations of members are only allowed inline.

    And that's all there is to it :-)

  9. #9
    Join Date
    Feb 2005
    Posts
    1

    Red face Re: Function Templates within Class possible ??

    hello,

    I am facing quite similar problem but with one difference. Some of the template functions are working and not producing compilation errors while some are not. Also, the template functions which are compiling does not contain the use of template variable T.

    I am using VC++6

    thanks in advance.

    Regards
    Vikas

  10. #10
    Join Date
    Aug 2001
    Location
    Israel
    Posts
    101

    Re: Function Templates within Class possible ??

    If a template func doesn't use T arguments, the VS6 compiler will not be able to instantiate it. It's in the MSDN. Send a dummy T argument to solve this:

    template <class T>
    T func(T dummy = 0) {...}

    I used the default value '0'. There is a drawback though: it REQUIRES that all T types used here will have a ctor that looks like this: T(int) {...}
    This is true even if you use something other than 0, like NULL, which will require a ctor: T(void*) { ...} and so on.

    If you don't want to use this trick, move to GNU or VS7 :-)

    About the other thing you said, I didn't understand what compiles and what doesn't. Please send some code.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured